从props默认(VueJS)访问此/ vm实例

Geo*_* C. 2 vue.js vuejs2

我有一个插件,可以为vue的对象原型设置一些变量。

我需要从道具的默认属性访问这些变量。我该如何实现?

使用以下示例,webpack会引发一些未定义的错误。

//...
props: {
    size: {
        type: String,
        required: false,
        default: this.$myPlugin.size
    }
}
Run Code Online (Sandbox Code Playgroud)

ski*_*tle 5

您可以将其指定default为返回默认值的函数。那应该可以访问当前实例this

props: {
    size: {
        type: String,
        required: false,
        default () {
            return this.$myPlugin.size
        } 
    }
}
Run Code Online (Sandbox Code Playgroud)

如果您感到好奇,请在此处找到 Vue源代码中的相关行。请注意,该函数vm以其this值显式调用。