Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access this / vm instance from props default (VueJS)

Tags:

vue.js

vuejs2

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
    }
}
like image 694
Geo C. Avatar asked Apr 25 '26 07:04

Geo C.


1 Answers

For Vue 2, you can specify 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.

like image 102
skirtle Avatar answered Apr 27 '26 07:04

skirtle



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!