如果我有两个这样的计算属性,
computed: {
id: function(){ return this.$route.query.id; },
hasId: function(){ return this.$route.query.id !== undefined; }
}
我如何使用 id
来计算 hasId
就像这个伪代码一样?
computed: {
id: function(){ return this.$route.query.id; },
hasId: function(){ return id !== undefined; }
}
原文由 Konrad Viltersten 发布,翻译遵循 CC BY-SA 4.0 许可协议
您需要使用正确的范围来访问 vue 计算属性。
当您只使用
id
时,它将在全局范围内搜索它但找不到它并返回未定义。要获取 vue 计算属性,您需要执行:this.id
,因此您的代码将如下所示:在组件内部,
this
指的是我们的 Vue 实例。但是,您可以从this
访问 $route 和其他类似功能,因为它们在Vue.prototype
中定义,请参阅以下来自 vue-router 的 代码: