Kon*_*ten 15 javascript vue.js
如果我有这样的两个计算属性,
computed: {
id: function(){ return this.$route.query.id; },
hasId: function(){ return this.$route.query.id !== undefined; }
}
Run Code Online (Sandbox Code Playgroud)
我怎样才能像这个伪代码那样id计算hasId?
computed: {
id: function(){ return this.$route.query.id; },
hasId: function(){ return id !== undefined; }
}
Run Code Online (Sandbox Code Playgroud)
Sau*_*abh 16
您需要使用正确的范围来访问vue计算属性.
因为你正在使用它id,它将在全局范围内搜索它而不是找到它并将返回undefined.要获得vue计算属性,您需要执行:this.id,因此您的代码将如下所示:
computed: {
id: function () { return this.$route.query.id; },
hasId: function () { return this.id !== undefined; }
}
Run Code Online (Sandbox Code Playgroud)
在组件内部,this指的是我们的Vue实例.然而,你可以访问从$航线和其他类似的功能this,因为它们是在定义Vue.prototype从,见下面的代码VUE路由器:
Object.defineProperty(Vue.prototype, '$route', {
get: function get$1 () { return this.$root._route }
})
Run Code Online (Sandbox Code Playgroud)
你的伪代码非常接近。只需更改id为this.id
computed: {
id: function(){ return this.$route.query.id; },
hasId: function(){ return this.id !== undefined; }
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11880 次 |
| 最近记录: |