Call Mixin global method from Vuex
13,268
Solution 1
Mixins, as stated from the docs, are used for components. Vuex is not a component itself. As I can see you're using the new import/export ways of working, just make your mixing a simple functions that are exported. Then elsewhere either attach them to Vue as mixin, or use it externally in the store. Something along the lines (semi code):
// mixin.js
export default function request() {}
// main.js
Vue.mixin({
methods: {
request: mixin.request // will provide this.request in each component
}
}
// store.js
mixin.request() // will fire the method
Solution 2
Since the request method is in the mixin's methods obj
// store.js
import mixin from './mixin'
// access mixin method
mixin.methods.request()
Author by
alxb
Updated on June 15, 2022Comments
-
alxb almost 2 years
I have a mixin like this with a request method to call axios and handle errors, etc.:
import Vue from 'vue' import axios from 'axios'; Vue.mixin({ methods: { request(url, datas) { //Call axios and return premise [...] } });
I have a store like this :
const actions = { selectEmployees: (store, keywords) => { this.request(`/users/list/search{/keywords}`).then(e => { store.commit('SELECT_EMPLOYEES', e.data) }); } } let store = new Vuex.Store({ state: state, mutations: mutations, getters: getters, actions: actions })
I would like to use request to call axios but I have this error:
Error in mounted hook: "TypeError: Cannot read property 'request' of undefined" TypeError: Cannot read property 'request' of undefined
-
Jonas D. over 5 yearsAnd how would you obtain the mixin within store.js? 🤔
-
Andrey Popov over 5 yearsAs
import mixin from './wherever/the/file/is';
-
Marcelo Rodovalho about 5 yearsAnd if I want to call the Global Mixin form a vue instance?
vm = new Vue()
vm.mixinMethod()
?? -
matrix4use almost 5 yearsIN THIS CASE if you
import mixin from './wherever/the/file/is';
then you can't domixin.request()
as suggested above, rather you would call it asmixin()
to fire the function. -
Cathy Ha over 4 yearsNot sure about best practice or whatnot but this worked like a charm
-
Emobe over 4 yearsThis defeats the purpose of mixins.
-
ManUtopiK about 3 yearsYou named
main.js
the file where the mixin is instantiated, andmixin.js
the file where are the shared functions. It should be inverse? It's not very clear... -
Andrey Popov about 3 yearsWell
mixin.js
is the file where the mixin is defined, whilemain.js
is the place where the mixin is used. It makes sense to name the files based on what is the main idea of the code inside, right? -
vikash almost 3 yearsWay to access methods is perfectly working fine for me also, but the $data variable is not accessible of mixin.js from store.js. could you please help me here?
-
Michael over 2 years@Emobe Can you elaborate on why this defeats the purpose of mixins and offer a solution that does not?
-
Emobe over 2 years@Michael Mixins are to be used within a component's
mixin
property. This is just essentially a helper/utility.