If I have a two computed properties like this,
computed: {
id: function(){ return this.$route.query.id; },
hasId: function(){ return this.$route.query.id !== undefined; }
}
how can I use id
to compute hasId
like this pseudo-code?
computed: {
id: function(){ return this.$route.query.id; },
hasId: function(){ return id !== undefined; }
}
In Vue. js, computed properties enable you to create a property that can be used to modify, manipulate, and display data within your components in a readable and efficient manner. You can use computed properties to calculate and display values based on a value or set of values in the data model.
Conclusion: The computed property is a very powerful feature of Vue. js, and we have learned that it comes in handy when we have to change them when their dependencies get changed. We have learned to pass the parameter and use it in the computed property.
No, you can't pass parameters to computed properties.
Instead of a computed property, we can define the same function as a method. For the end result, the two approaches are indeed exactly the same. However, the difference is that computed properties are cached based on their reactive dependencies.
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; } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With