'Access this / vm instance from props default (VueJS)
I have a plugin that sets some variables to vue's object prototype.
I need to access these variables from a prop's default property. How can I achieve this?
Using the following example, webpack throws some undefined error.
//...
props: {
size: {
type: String,
required: false,
default: this.$myPlugin.size
}
}
Solution 1:[1]
You can specificy the default
as a function that returns the default value. That should have access to the current instance as this
.
props: {
size: {
type: String,
required: false,
default () {
return this.$myPlugin.size
}
}
}
The relevant line in the Vue source code is here if you're curious. Note that the function is explicitly called with vm
as its this
value.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |