A vue global method is defined, which cannot be called in vuex.

defines a global method of vue, but it can be called in the component, but it cannot be called in vuex. Prompt undefined

Vue.prototype.ajax = function (){
    alert("aaaaaaa");
};

var store = new Vuex.Store({
    state: {
        user: {},
    },
    actions: {
        signout: function(context) {
            this.ajax();
            //undefined
        }
    }
});
Mar.12,2021

reference:


here this how can it be Vue is an instance, and Vuex is not used in this way.


you define the function on the prototype chain of Vue , and you can only get this method in the instance of Vue .

The

vue component is an instance of Vue , so of course you can call the ajax method here.

while vuex is just a vue plug-in , the this in vuex does not point to the of the vue instance, so there is definitely no ajax method.

suggestion: the ajax function is defined in a separate module, so that you can introduce and use it in different parts of the project in the form of import .


your method is defined on the Vue prototype chain, which is certainly not used in the Vuex instance.

in addition, this method does not need to be hung on Vue , just write a function import to come in and use it.


step on the problem, give me some advice, don't just step on it

Menu