计算属性返回Boolean值
在Vue 2中,计算属性应当始终返回一个值,以确保它的行为一致性。从你的代码片段来看,当条件满足时,isFile 计算属性返回 true,但当条件不满足时,并没有返回值,这在JavaScript中意味着它会返回 undefined,这可能不是你想要的行为。
为了确保 isFile 总是返回一个布尔值,你可以简化你的代码如下:
computed: {
isFile() {
const allowStatuses = ["Pending_modify", "Modifying"];
// 使用逻辑非(!)操作符两次可以将任何值转换为布尔值
return !!allowStatuses.includes(this.itemNodeInfo.properties.status);
}
}
在这里,includes 方法会检测 this.itemNodeInfo.properties.status 是否在 allowStatuses 数组中。如果是,includes 方法返回 true,否则返回 false。使用 !! 是一种常见的JavaScript技巧来将一个值强制转换成布尔值,确保计算属性 isFile 总是返回 true 或 false。
这样,无论 this.itemNodeInfo.properties.status 的值是什么,isFile 都将返回一个明确的布尔值。