How can I call method from data on vue.js?
When accessing data or methods from within the vue object, use this.thing
. In your case, that would be this.strSlug(this.shop.name)
.
If you have a function in data
that will be used in a context of another object (like an event handler, for example), then this
will not point to the Vue instance. You will have to preserve the reference in another variable from the scope of data()
:
methods: {
shuffle() {}
},
data() {
var self = this;
return {
onClick: function() {
self.shuffle()
}
}
}
Does not work even with 'this.' because that function has not been defined at the time data is being initialized. I think you have to do it in the created() life-cycle hook.