亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在使用 setTimeout 函數時,在 Vue 中使用 v-show 或 v-if 切換可見性

在使用 setTimeout 函數時,在 Vue 中使用 v-show 或 v-if 切換可見性

慕無忌1623718 2023-07-06 15:03:02
我正在使用Vue2。我有一個表格。單擊提交時,我想顯示“正在加載”Div X 時間。經過 X 時間后,我想再次加載表單。簡而言之,單擊后,切換可見性,執行某些代碼并等待一定時間后,再次切換它。我有兩個 Div:<div v-if="this.isHidden"> LOADING....</div><div v-if="!this.isHidden"> <!--or v-else -->  <form @submit.prevent="">  <!--Fields-->  <button @click="updateProduct(product)" type="button">Submit</button>  </form></div>加載時,僅顯示“CONTENT”(isHidden = false)。當執行 updateProduct() 方法時,我希望 div“LOADING”顯示 X 秒(isHidden=true),而“CONTENT”div 必須隱藏。然后在 setTimeout 函數內執行一些代碼后,我希望它們再次切換(isHidden = false)。data: function() {   return {     //isHidden is initialized as false     isHidden: false,   };},我的方法如下所示:updateProduct(product){  this.isHidden = true;  alert(this.isHidden + 'This correctly shows true. CONTENT correctly toggles and HIDES. LOADING also works correctly and APPEARS.');  //I want to delay the following code with setTimeout so that I am able to control for how long I want to show LOADING  setTimeout(function(){    //do a bunch of tasks that work correctly...     axios.post('/api/product/' + product.id, data)       .then(function (response) {       console.log(response);     }).catch(function (error) {       console.log(error);                 });    //After all my code is executed, I am still inside the setTimeout function inside the updateProduct method so now i do this:    this.isHidden = false;    alert(this.isHidden + ' This correctly shows false BUT DIVS DO NOT UPDATE ANYMORE, I am stuck with the LOADING div');   //although my code works fine and the alert shows the correct value for isHidden, my view is stuck in LOADING  }, 2000);}我試圖移動 this.isHidden = false; 在 setTimeout 函數之外,但它仍然不會“切換”可見性。我還嘗試使用 v-if 而不是 v-show,行為相同。
查看完整描述

2 回答

?
HUWWW

TA貢獻1874條經驗 獲得超12個贊

您的主要問題this在 setTimeout 回調內部


因為你使用的function() {}不是你想要this的


要么使用箭頭表示法setTimeout(() => {}, 2000)


updateProduct(product) {

  this.isHidden = true;

  setTimeout(() => {

    //do a bunch of tasks that work correctly... 

    axios.post('/api/product/' + product.id, data)

      .then(function(response) {

        console.log(response);

      }).catch(function(error) {

        console.log(error);

      });

    this.isHidden = false;

  }, 2000);

}

或者,如果箭頭符號讓您害怕setTimeout(function() {}.bind(this), 2000)


updateProduct(product) {

  this.isHidden = true;

  setTimeout(function() {

    //do a bunch of tasks that work correctly... 

    axios.post('/api/product/' + product.id, data)

      .then(function(response) {

        console.log(response);

      }).catch(function(error) {

        console.log(error);

      });

    this.isHidden = false;

  }.bind(this), 2000);

}

順便說一句,不要用于alert調試 - 它會阻止 javascript 的執行,并可能導致不可靠的調試結果


查看完整回答
反對 回復 2023-07-06
?
料青山看我應如是

TA貢獻1772條經驗 獲得超8個贊

只需使用 V-else


<div v-if="!this.isHidden"> <!--or v-else -->

  <form @submit.prevent="">

  <!--Fields-->

  <button @click="updateProduct(product)" type="button">Submit</button>

  </form>

</div>


<div v-else>

<div v-if="this.isHidden">

 LOADING....

</div>

</div>


查看完整回答
反對 回復 2023-07-06
  • 2 回答
  • 0 關注
  • 212 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號