第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

VueJS - 不能在異步函數(shù)之外使用關(guān)鍵字'await'

VueJS - 不能在異步函數(shù)之外使用關(guān)鍵字'await'

呼啦一陣風(fēng) 2022-10-13 19:23:43
我有這段代碼,我收到了這個(gè)錯(cuò)誤——“不能在異步函數(shù)之外使用關(guān)鍵字'await'”:async login() {                try {                    this.$validator.validateAll().then((result) => {                        if (result) {                            this.state = 'LOADING';                            this.response = [];                            const authResponse = await axios.post('/api/auth/login', this.user);                            const auth = authResponse.data;                            if (auth.success) {                                this.$authLoggedIn(auth);                                this.$authRedirectToDefault();                            } else {                                this.response.push(auth.error);                                this.state = 'ERROR';                            }                        }                    });                } catch (error) {                    this.state = 'ERROR';                    this.response.push(error);                }            },我該如何解決這個(gè)問(wèn)題?被困在這幾個(gè)小時(shí)了。
查看完整描述

3 回答

?
湖上湖

TA貢獻(xiàn)2003條經(jīng)驗(yàn) 獲得超2個(gè)贊

你不需要自己解開承諾.then,因?yàn)槟阋呀?jīng)在一個(gè)異步函數(shù)中。當(dāng)您使用.then((result) => {...})該箭頭功能時(shí)不再是異步的。我的建議:


  try {

    const result = await this.$validator.validateAll()

    if (result) {

      this.state = 'LOADING';

      this.response = [];

      const authResponse = await axios.post('/api/auth/login', this.user);

      const auth = authResponse.data;


      if (auth.success) {

        this.$authLoggedIn(auth);

        this.$authRedirectToDefault();

      } else {

        this.response.push(auth.error);

        this.state = 'ERROR';

      }

    }

  } catch (error) {

    this.state = 'ERROR';

    this.response.push(error);

  }

或者,您可以通過(guò)執(zhí)行以下操作將箭頭函數(shù)標(biāo)記為異步:


this.$validator.validateAll().then(async (result) => {


查看完整回答
反對(duì) 回復(fù) 2022-10-13
?
蕭十郎

TA貢獻(xiàn)1815條經(jīng)驗(yàn) 獲得超13個(gè)贊

我們知道這async..await是對(duì)關(guān)鍵字,


只是您async在上層函數(shù)中使用過(guò)。您的內(nèi)部函數(shù)沒有 async 關(guān)鍵字,但您await在函數(shù)內(nèi)部使用過(guò)。這就是為什么它顯示錯(cuò)誤。只是我標(biāo)記了不是的功能async


(result)/*this function has not async*/ => {

                        if (result) {

                            this.state = 'LOADING';

                            this.response = [];

                            const authResponse = await axios.post('/api/auth/login', this.user);

                            const auth = authResponse.data;


                            if (auth.success) {

                                this.$authLoggedIn(auth);

                                this.$authRedirectToDefault();

                            } else {

                                this.response.push(auth.error);

                                this.state = 'ERROR';

                            }

                        }

                    });

所以你必須在內(nèi)部函數(shù)中使用 async 關(guān)鍵字。


async (result) => { /* 你的代碼在這里 */ }


查看完整回答
反對(duì) 回復(fù) 2022-10-13
?
慕斯王

TA貢獻(xiàn)1864條經(jīng)驗(yàn) 獲得超2個(gè)贊

登錄函數(shù)已經(jīng)是異步函數(shù),你可以用 await 調(diào)用 this.$validator 而不是 .then()。因?yàn)閮?nèi)部 .then(() => {}) 是另一個(gè)回調(diào)函數(shù)而不是異步函數(shù)。這是我的建議:


try {

    let result = await this.$validator.validateAll();

    if (result) {

      this.state = 'LOADING';

      this.response = [];

      const authResponse = await axios.post('/api/auth/login', this.user);

      const auth = authResponse.data;


      if (auth.success) {

        this.$authLoggedIn(auth);

        this.$authRedirectToDefault();

      } else {

        this.response.push(auth.error);

        this.state = 'ERROR';

      }

    }

  } catch (error) {

    this.state = 'ERROR';

    this.response.push(error);

  }```


查看完整回答
反對(duì) 回復(fù) 2022-10-13
  • 3 回答
  • 0 關(guān)注
  • 265 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)