Can I rely on this
used inside data factory function as it was current ponent object instance? I couldn't find in docs what this
is in data()
.
data() {
return {
results: [],
apiResource: this.$resource('url...'), // <-- this used here
loading: true,
}
},
Simple test shows that this
is VueComponent
instance here, but the question is if the framework allows using it this way.
Can I rely on this
used inside data factory function as it was current ponent object instance? I couldn't find in docs what this
is in data()
.
data() {
return {
results: [],
apiResource: this.$resource('url...'), // <-- this used here
loading: true,
}
},
Simple test shows that this
is VueComponent
instance here, but the question is if the framework allows using it this way.
2 Answers
Reset to default 6Yes, you can rely on this
in the data factory function pointing to the ponent, depending on how you define the function. It's the primary way of initializing local data with values from properties, for example.
props:["value"],
data(){
return {
localValue: this.value
}
}
If, however, you defined your data function with an arrow function, this
will not be the ponent.
props:["value"],
data: () => {
// 'this' is NOT the ponent
return {
localValue: this.value // results in undefined
}
}
I think no Perhaps you need
data() {
return {
results: [],
set apiResource(v){},
get apiResource()( return this.$resource('url...')), // <-- this used here
loading: true,
}
},