vue3.0
ref
如下片断:GlobalSearchInfo.value.searchInfo = ""
这句类型怎么定义?
<child ref="GlobalSearchInfo"> </child>
const GlobalSearchInfo = ref<null | HTMLElement>(null);
GlobalSearchInfo.value.searchInfo = "";
提示:
Object is possibly 'null'.
Property 'searchInfo' does not exist on type 'HTMLElement'
state.menuData = menuData.concat(checkData)
这行标红提示如下:const state = reactive<DataProps>({ menuData: [{ id: "", systemNm: "", count: 0, checked: false }], topicData: [], totalNum: 0, }) const topicData = [ { id: "001", subjAreaNm: "不限", count: 0, checked: true }, ]; const menuData = [ { id: "001", systemNm: "全部", count: 0, checked: true }, ]; function addChecked(data) { for (let i = 0; i < data.length; i++) { data[i].checked = false; } return data; } // 请求返回的数据添加checked const checkData = addChecked(res.data); state.menuData = menuData.concat(checkData);
提示:
Type '{ id: string; subjAreaNm: string; count: number; checked: boolean; }[]' is not assignable to type '[]'.
Target allows only 0 element(s) but source may have more.
- 给
totalNum
赋值:state.totalNum = res.total;
提示:
Property 'total' does not exist on type 'AxiosResponse<any, any>'
state.topicData
,checked
需要类型:
const onRestoreTopicSelect = () => {
for (let i = 0; i < state.topicData.length; i++) {
if (i === 0) {
state.topicData[i].checked = true;
} else {
state.topicData[i].checked = false;
}
}
};
提示:
Object is possibly 'undefined'
Property 'checked' does not exist on type 'never'.
提示给出的信息是
GlobalSearchInfo.value
可能是null
,这样你GlobalSearchInfo.value.searchInfo
就会报错,第二个提示是说HTMLElement
这种类型上没有searchInfo
这个属性如果你是想修改组件里的data或者调用里面的方法 可以这样
应该是你
addChecked
的返回值推断不出来是什么类型,你补充下类型试试AxiosResponse
类型给一下,res
是什么样,提示的消息是说AxiosResponse<any, any>
不能确定有没有total
属性提示基本也是说你类型没定义好,
topicData
的topicData[i]
有可能是undefined
,提示二never
说明肯定是topicData[i]
可能是个never
,而never
上肯定不包含checked
这个属性