鍍金池/ 問答/HTML/ vue中 鉤子函數(shù)如何使用async?

vue中 鉤子函數(shù)如何使用async?

  async created () {
    await setTimeout(()=>{
      console.log(1)
    },5000);
  },
  async mounted () {
    console.log(2)
  }

在vue中給created使用async await,還是會先輸出2,而不是等1輸出完?

回答
編輯回答
她愚我

你要理解,所有的鉤子函數(shù)都只是在指定時(shí)間執(zhí)行而已,框架並不關(guān)心它們執(zhí)行的結(jié)果,所以你這種做法無法滿足你的需求。

建議你根據(jù)需求調(diào)整實(shí)現(xiàn)。

2017年6月27日 17:41
編輯回答
傻丟丟

可以變相達(dá)到這個(gè)目的

  async created () {
    this.create_promise = new Promise(resolve=>this.create_promise_resolve=resolve);
    setTimeout(()=>{
        console.log(1);
        this.create_promise_resolve();
    },1000)
  },
  async mounted () {
    await this.create_promise;
    console.log(2)
  }
2018年3月3日 00:06