Is it possible to use the computed properties to compute another properties in Vue?
You need to use correct scope to access a vue computed property.
as you are using just id
, it will search it in global scope and not find it and will return undefined. For getting vue computed property, you need to do: this.id
, so your code will look like following:
computed: {
id: function () { return this.$route.query.id; },
hasId: function () { return this.id !== undefined; }
}
Inside a component, this
refers to our Vue instance. However you can access $route and other similar function from this
, as they are defined at Vue.prototype
, see below code from vue-router:
Object.defineProperty(Vue.prototype, '$route', {
get: function get$1 () { return this.$root._route }
})
your pseudo code was very close. Just change id
to this.id
computed: {
id: function(){ return this.$route.query.id; },
hasId: function(){ return this.id !== undefined; }
}