Vue 2:无法过去"无法阅读财产"

时间:2017-01-15 14:59:16

标签: vue.js vuex

我刚将我的应用升级到Vue 2,似乎无法解决此错误:

  

"未捕获的TypeError:无法读取属性'长度' null的   VueComponent.showChoices(eval at(app.js:310),   :33:32)”

。我以为我是用条件保护我的,但这个错误现在困扰了我3个小时。这是showChoices:

showChoices() {
    if(this.filtered) {
        if (this.search.length > 2 && this.filtered.length > 0) {
            return true;
        }
    }
    return false;
}

2 个答案:

答案 0 :(得分:1)

null值没有length属性。 正如您所评论的那样this.search为空。你不是故意这样做的吗?:

showChoices() {
  if (this.search && this.filtered) {
    if (this.search.length > 2 && this.filtered.length > 0) {
        return true;
    }
  }
  return false;
}

答案 1 :(得分:0)

您还需要添加空检查,如下所示:

showChoices() {
    if (this.search && this.search.length > 2 && this.filtered && this.filtered.length > 0) {
      return true;
    }
    return false;
}
相关问题