# mpx/return-in-computed-property
强制返回语句存在于计算属性中
- ⚙️ 这条规则包含在
"plugin:mpx/mpx-essential"。
# 📖 规则详情
此规则强制在 computed 属性中存在 return 语句。
<script>
createComponent({
computed: {
/* ✓ GOOD */
foo () {
if (this.bar) {
return this.baz
} else {
return this.baf
}
},
bar: function () {
return false
},
/* ✗ BAD */
baz () {
if (this.baf) {
return this.baf
}
},
baf: function () {}
}
})
</script>
# 🔧 选项
{
"mpx/return-in-computed-property": ["error", {
"treatUndefinedAsUnspecified": true
}]
}
此规则有一个对象选项:
"treatUndefinedAsUnspecified":true(默认)不允许使用return语句隐式返回 undefined。
# treatUndefinedAsUnspecified: false
<script>
createComponent({
computed: {
/* ✓ GOOD */
foo () {
if (this.bar) {
return undefined
} else {
return
}
},
bar: function () {
return
},
/* ✗ BAD */
baz () {
if (this.baf) {
return this.baf
}
},
baf: function () {}
}
})
</script>