【发布时间】:2019-09-15 07:40:16
【问题描述】:
我正在使用带有 vue 的 Typescript。问题是我已经声明了一个名为 categoryList 的实例属性,当组件在运行时使用来自 api 调用的数组呈现时,该属性将被填充。所以,当我在另一个方法中引用这个属性时:
this.categoryList.length && this.categoryList[0] 因为我知道这在方法执行时会有一些价值
TS 给了我一个警告。
由于 categoryList 将是一个 Object 数组,如果我这样访问它
this.categoryList[0].source_id
我收到以下警告
相反,如果我像这样访问它this.categoryList[0]
我收到以下警告
但是,如果将来在运行时将值分配给被引用的实例属性,我该如何避免这种警告。
class Alerts extends Vue {
private activeView: number = VIEW.NEW;
private categoryList: [] = [];
mounted() {
this.fetchCategories()
}
/*
* method to fetch from an api call
*/
fetchCategories() {
this.$axios.get(CATEGORY_URL).then((res) => {
categoryList = res.data
})
}
doSomethingWithCategories() {
// have to use categoryList here
const use = this.categoryList.length && this.categoryList[0] // This warns me that Object is possibly undefined
// ...
}
}
正如@Mark 所建议的,我已经在使用条件来确保值的可用性,但仍然收到警告。
【问题讨论】:
-
发布您收到的准确和完整的错误/警告。
-
一方面,您已将您的
categoryList声明为private categoryList: [] = []中的一个空数组。那么categoryList = res.data也应该因为分配无效而给你一个错误(但也可能因为它应该是this.categoryList = res.data)。我们需要查看实际代码(或MCVE),正如 JB 所说,实际错误。 -
我正在使用 axios 并且 res 的类型为
AxiosResponse<any>而 res.data 的类型为any。因此,将其分配给 categoryList 不会弹出任何警告。 -
编译器似乎没有考虑布尔运算的左边部分。它确实理解
if块,所以我会尝试将它包含在if(this.categoryList[0] !== undefined) {...}之类的条件中,甚至可能是if(this.categoryList.length > 0) {...}。也可以使用this.categoryList.forEach(...)工作,只需获取第一个元素并退出循环。 -
if(this.categoryList.length > 0) {...}与if(this.categoryList.length){...}工作方式相同,因为 0 是falsey。此外,即使在条件检查中,似乎按索引访问属性也会弹出警告。`this.categoryList.forEach(...)` 似乎有点骇人听闻,但摆脱了警告
标签: typescript vue.js vuejs2 axios