简体   繁体   中英

is that correct to call an axios method inside created?

I'm working on a small project using VueJS and I would like to know if my code is considered as a good practice:

created(){
    this.fetch()
}
methods: {
    fetch(){
       // axios request
    }
}

You are calling a Vue method from the created life cycle hook. That's perfectly fine

Here there is few examples of the same thing that you are doing:

The link is from the VueJs docs itself.

Yes, they are doing the same things.

if you plan to add more stuff into your created block which could depending your fetched data then you should keep in mind you have to await your data

async created(){
    await this.fetch()
    // other stuff here which needs the data above
}
methods: {
    fetch(){
       // axios request
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM