[Vuejs]-Vue variables are not set after I get them from api

1👍

mounted is synchronous. loadUsers is doing something asynchronous, but is executing it through a synchronous function. Either turn both of them into an async function and use await or have loadUsers return a promise that’s resolved inside the then, which the mounted function then also assigns the value to this.users.

async method

methods: {
        async loadUsers() {
            let { data } = await axios.get('/karma/post-users-chart')
            this.users = data.users
            this.labels = data.labels
            this.max = data.max
        },
    },


async mounted() {
    await this.loadUsers();
    console.log(this.users); // it returns null
}
👤A. L

Leave a comment