Vue mounted failed to read data in data

clipboard.png

createdthis.lasttime;

clipboard.png

mounted this.lasttimedata

clipboard.png

how can the data in created be printed in mounted

Jul.06,2021

is for asynchronous reasons.
mounted is a hook function that is called when the vue component is loaded, which should not be written in mounted.

1. If you want to use lastime, immediately after the interface returns, you should write a methods, for this specific logic and call it in the callback of the interface.
2. It's OK to write the API request function as Synchronize, but this has a cost, because you call it in created. If the request for this API is slow or there are some other problems, it will block the rendering and mounting of this component

.

is the reason for asynchrony?

methods:{
    async query(){
        const res = await this.$axios.get(url2)
        ...
        this.lasttime = this.act ...
        console.log(this.lasttime)
    }
}
created(){
   this.query()
}
mounted(){
    console.log(this.lasttime)
}

try this


Hello, you did not save the data to the Vue instance in create, and using this, in axios is not a Vue instance, so when you this.lasttime =, you create a lasttime attribute for axios and assign a value to axios, not asynchronously.

Menu