vue.js typescript 版本:类型“Users[]"缺少类型”ArrayConstructor“的以下属性:isArray,原型,来自,属于,[符号.种类]

6kkfgxo0  于 2023-01-02  发布在  Vue.js
关注(0)|答案(2)|浏览(266)

我正在用vuejs启动 typescript ,我刚刚遇到了这个错误,我已经坚持了2天。错误如下:第一个月
下面是我的GetAll.controller.ts代码:

import { supabase } from "../server";

type Users = {
    user_id: Number,
    user_username: String,
    user_email: String,
    user_password: String,
    user_token: String
}

export async function GetAll():Promise<Users[]> {
    console.log('go');
    
    try {

        let { data, error } = await supabase
            .from('users')
            .select('*')
        if (error) throw error
        if (data) {
            return data
        } else {
            throw data
        }

    } catch (err) { throw(err); }
}

我的App.vue

<script lang="ts">
import {GetAll} from './API/GetAll.controller';

export default {
  name: 'app',
  data(){
    return {
      users: Array
    }
  },
  async mounted() {
    this.users = await GetAll()
  }
}
</script>

错误低于mounted()内部的this.users

8yparm6h

8yparm6h1#

你应该给予你的用户正确的初始化值,如果你没有定义关于用户的类型,typescript会把你指定的初始化值当作默认的变量类型。

...
  data(){
     return {
      users: [] as Users[]
    }
  }
...
0qx6xfy6

0qx6xfy62#

非常感谢,它解决了,这里是答案:

<script lang="ts">
import {GetAll, Users} from './API/GetAll.controller';

export default {
  name: 'app',
  data(){
     return {
      users: [] as Users[]
    }
  },
  async mounted() {
    this.users = await GetAll()
  }
}
</script>

相关问题