【问题标题】:Mixin not working Vue.js - Error: {method} refrenced but not definedMixin 不工作 Vue.js - 错误:{method} refrenced but not defined
【发布时间】:2020-02-03 19:15:29
【问题描述】:

我的 Vue 项目中有一个 mixin。但是,它不起作用。 代码如下:

InvMixin.js

export default {
    created() {
        console.log("hi");
    },

    methods: {
        helloa() {
            console.log('hi from mixin');
        },
        proal() {
            console.log("hoye");
        }
    }
}

模板

import invMixin from "./InventoryMixin.js";
export default {

  mixins: [invMixin],

 methods: {
        check() {
            this.helloa();
        }
    }
}

输出

helloa 被引用但未定义 创建的 mixin 不起作用

有人可以帮忙解决吗?谢谢!!

【问题讨论】:

  • 请多分享一些 template.vue,特别是在导入和方法之间
  • mixin的真名是什么? InventoryMixininvMixin,尝试只使用一个
  • InventoryMixin.js 的路径是否正确?只是要求清楚。
  • 这应该可以很好地工作,除非您发布的代码与您使用的代码不同。我遇到的唯一与您类似的情况是,我不小心将 mixins:[] 放在了没有引发错误的道具中。

标签: vue.js vuejs2 mixins


【解决方案1】:

仅仅导入你的 mixin 是不够的。在你的组件中声明它:

// In your component
import invMixin from "./InventoryMixin.js";

export default {
  name: "MyComponent",
  mixins: [invMixin] //declare mixin here
}

【讨论】:

  • 完成了,还是一样
  • @idk imvMixin.jsInventoryMixin.js 有什么不同吗?并显示您使用 mixin 的完整组件
猜你喜欢
  • 2021-03-12
  • 2021-02-08
  • 1970-01-01
  • 1970-01-01
  • 2018-07-15
  • 2020-12-26
  • 2017-08-04
  • 1970-01-01
  • 2023-01-26
相关资源
最近更新 更多