在处理接口返回的数据,赋值给store中的数组时,报类型错误
data:{
cateList: []
}
const getCateList = async () => {
const res = await fetchCateList() as any
if (res.code == 200) {
this.cateList = res.data
}
res已经定义了any类型,但是cateList的类型还没有定义,默认为never
data: {
cateList: <any>[]
},
data: {
cateList: [] as any
},
//接口返回的数据结构如下
res.data = [
{id:'1',name:'xxx',level:1},
{id:'2',name:'xxx',level:2},
{id:'3',name:'xxx',level:3}
]
//根据返回的数据结构定义对应的类型
interface ICateList {
id: string
name: string
level:number
}
data: {
cateList: [] as ICateList[],
},