javascriptvue.jsvuejs2vue-componentvue-mixin

How to use Vue mixin methods in the component


I'm trying to create a mixin in Vue.js v2.x but every time I call one of its methods, the console shows an error:

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();
    }
  }
}

Where am I wrong?


Solution

  • Access a mixin's properties using the same syntax as when defining properties natively. In other words, use this instead of the mixin object:

    methods: {
      onSubmit() {
        this.loadSettings();
      }
    }