1

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

3 Answers 3

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

I already tried this, but its not working.
0

loading should be this.loading, as another answer shows. Otherwise it accesses global variable that doesn't exist and isn't specific to a component, also not reactive.

getEntityList is arrow function. The choice between regular and arrow function should always be justified. Here an arrow will prevent from using correct this context.

then doesn't need to be used together with async..await, the latter is supposed to replace it.

It should be:

    async getEntityList(entityName) {
        this.loading = true 
        let response = await axios
            .get("/api/" + entityName);
        this.loading = false;
        return response.data;
    },

Comments

0

A tip, to help! In the browser console, you can put the name of the Vue instance, in your case it is > Vue.mixin. One more thing you didn't instantiate anything like below. For example:

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

ok or try something like this.

data: {
  loading: false,
  methods:{
  loadingFunction: function () {
    this.loading = false;
    // or return this.loading
  }
 }
}

3 Comments

Its instantiated, everything works, I can access this loading variable inside a vue component, but not inside the mixin.
@HarisurRehman I put a code above editing my comment, try to do something like I put.
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.