I believe I have a scenario that helps illustrate my question.
It's important to note that arrow functions cannot be used on certain properties or callbacks, like
created: () => console.log(this.a)
or
vm.$watch('a', newValue => this.myMethod())
. Since arrow functions do not have their own
this
context, the keyword will be searched for in parent scopes, leading to potential errors such as
Uncaught TypeError: Cannot read property of undefined or Uncaught TypeError: this.myMethod is not a function.
To work around this issue with data properties, one approach is to utilize an arrow function where the component's instance can still be accessed as the function's first argument:
var data = { a: 1 }
// Direct instance creation
var vm = new Vue({
data: data
})
vm.a // => 1
vm.$data === data // => true
// Function must be used with Vue.extend()
var Component = Vue.extend({
data: vm => ({ a: vm.myProp })
})
However, it's worth noting that this is not the conventional practice. Usually, data properties in components are declared as functions to allow direct access to the Vue instance.
// Must use function with Vue.extend()
var Component = Vue.extend({
data: function () {
return { a: 1 }
}
})