2

I would like to call axios with this.request(url) from a mixin (to simplify and centralize everything about axios in the same file) but it's not working.

Vue file:

export default {
  name: "employees-list",
  data() {
    return {
      employees: []
    }
  },
  mounted() {
    this.employees = this.request('https://jsonplaceholder.typicode.com/posts');
  }
}

Request.js

Vue.mixin({
  methods: {
    request: function (url) {
      axios.get(url)
        .then(response => {
        return response.data
      })
        .catch(e => {
        this.errors.push(e)
      })
    }
  }
});

The employees is "undefined".

I think the problem is async or await but i don't understand.

0

2 Answers 2

6

It looks like you want the mixin to create a generic method for retrieving data. That being the case you need to return the promise from the request method and handle the resulting data in the success callback.

Here is a working example.

console.clear()

const EmployeesList = {
  name: "employees-list",
  template: `
      <ul>
        <li v-for="employee in employees">{{employee.title}}</li>
      </ul>
    `,
  data() {
    return {
      employees: []
    }
  },
  mounted() {
    // obviously using posts here as an example instead of 
    // employees.
    this.request('https://jsonplaceholder.typicode.com/posts')
      // handle the promise success
      .then(e => this.employees = e);
  }
}

Vue.mixin({
  methods: {
    request: function(url) {
      // return the promise
      return axios.get(url)
        .then(response => {
          return response.data
        })
        .catch(e => {
          this.errors.push(e)
        })
    }
  }
});

new Vue({
  el: "#app",
  components: {
    EmployeesList
  }
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/axios/0.17.1/axios.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.13/vue.js"></script>

<div id="app">
  <employees-list></employees-list>
</div>

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

1 Comment

Great understanding of OPs needs and intentions, good explanation and beautiful code. <3
1

Try this:

new Vue({
    el: '#root',
  data: {
    employees: []
  },
  methods: {
    request(url){
        return new Promise((resolve, _) => {
        axios.get(url)
          .then(res => {
            resolve(res);
          }).catch(err => {
            // err management
          });
      });   
    }
  },
  mounted(){
   this.request('https://jsonplaceholder.typicode.com/posts').then(res => {
    this.employees = res;
   })
  }
})

4 Comments

employees has "Promise" value but when i do this : <li v-for="employee in employees">{{employee.title}}</li> it's not working
I changed my post. It should work better for you now @alxb
I think it will work but it's too much code. I want to have everything about axios only in one external file to do a simple call this.request() where i want in my vue file. I don't want to have for each vue file axios.get.... on methods. Do you see what i want to do? The final goal is i want to do only this.request(url) in one line and let request function do error management, etc...
Sorry was a bit blind before. :D now it should work perfectly

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.