Home > other >  Vue - Access Mixin Data from Mixin Method
Vue - Access Mixin Data from Mixin Method

Time:09-10

I have following piece of code and cannot seem to access the mixin data/variable loading from mixin method. What am I doing wrong here?

Vue.mixin({
    data: function () {
        return {
            loading: false,
        };
    },
    methods: {
        getEntityList: async (entityName) => {
            loading = true 
            return await axios
                .get("/api/"   entityName)
                .then((response) => {
                    loading = false;
                    return response.data;
                });
        },
    },
});

In Vue component, I am calling this function like:

export default {
  name: "somename",
  data() {
    return {
      accounts: [],
    };
  },

  mounted() {
    this.getEntityList('account').then(e => this.accounts = e);
  },
};

CodePudding user response:

in condition you are using the options API, you need to reference your loading oh yes, vue has a behavior where you need to use "this.function" to declare your functions etc.. with a this.

this.loading = true and this.loading = false

CodePudding user response:

a tip, to help.. in the browser console you can put the name of the vue instance, in your case it is > Vue.mixin. Another thing you didn't instantiate anything? for example this way

var app = new Vue({
            el: "#app",
            data: {
  • Related