如何在组件中使用 Vue mixin 方法
How to use Vue mixin methods in the component
我正在尝试在 Vue.js v2.x 中创建一个 mixin,但每次我调用其中一个方法时,控制台都会显示错误:
TypeError: mixins_settings_js__WEBPACK_IMPORTED_MODULE_3_.default.loadSettings is not a function
at eval (Login.vue?7463:102)
/src/mixins/setting.js
import vuex from "@/plugins/vuex.js"
import menu from "@/settings/menu.json";
export default {
methods: {
_traverse(jsonObj) {...},
loadSettings() {
this._traverse(menu);
...
}
}
}
/src/components/Login.vue
...
import settings from "@/mixins/settings.js"
export default {
mixins: [settings],
data() {
return {...};
},
methods: {
onSubmit() {
...
settings.loadSettings();
}
}
}
我哪里错了?
使用与本机定义属性时相同的语法访问 mixin 的属性。换句话说,使用 this
而不是 mixin 对象:
methods: {
onSubmit() {
this.loadSettings();
}
}
我正在尝试在 Vue.js v2.x 中创建一个 mixin,但每次我调用其中一个方法时,控制台都会显示错误:
TypeError: mixins_settings_js__WEBPACK_IMPORTED_MODULE_3_.default.loadSettings is not a function at eval (Login.vue?7463:102)
/src/mixins/setting.js
import vuex from "@/plugins/vuex.js"
import menu from "@/settings/menu.json";
export default {
methods: {
_traverse(jsonObj) {...},
loadSettings() {
this._traverse(menu);
...
}
}
}
/src/components/Login.vue
...
import settings from "@/mixins/settings.js"
export default {
mixins: [settings],
data() {
return {...};
},
methods: {
onSubmit() {
...
settings.loadSettings();
}
}
}
我哪里错了?
使用与本机定义属性时相同的语法访问 mixin 的属性。换句话说,使用 this
而不是 mixin 对象:
methods: {
onSubmit() {
this.loadSettings();
}
}