首页 > 解决方案 > 如何处理 Vue 中返回未定义错误的渲染错误

问题描述

我有下面的代码在运行时引发未定义的错误,无论如何我可以捕获或使用条件格式吗?这是我到目前为止所做的:

<data-list 
    label='Group Name: '
    :change="gotoGroups()"
></data-list> //this a search and select drop-down list

方法:

gotoGroups(){
    if(this.$refs['groupName'].choice != 'undefined'){
    //I have also tried (typeof this.$refs['groupName'].choice != 'undefined')
    //do this
    }
}

发出警告:

app.js:22462 [Vue 警告]:渲染错误:“TypeError:无法读取未定义的属性‘选择’”

两个都不行,怎么超越?

标签: if-statementvuejs2conditional-statements

解决方案


你可以摆脱这个TypeError。使用代码如下

if(this.$refs['groupName'] && this.$refs['groupName'].choice != 'undefined'){   
  //do this
}

要了解有关Short-circuit evaluation您的更多信息,可以查看此资源


推荐阅读