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

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

使用 Vue 3(組合 API)替換對象而不失去反應(yīng)性

使用 Vue 3(組合 API)替換對象而不失去反應(yīng)性

慕容3067478 2023-09-28 17:11:42
我想更新我的對象并保持反應(yīng)性。selectedTime是一個 Date 對象,substractMonths將返回一個新的 Date 對象。<span   class="calendar__pickable__chevron"   @click="selectedTime = substractMonths(1, selectedTime)"   >   <span>      <font-awesome-icon icon="chevron-left" />   </span></span>問題是,通過這樣做,我的狀態(tài)沒有更新,因為 Vue 3 無法檢測到對象的替換。我該如何解決這個問題呢?(我指定我已經(jīng)用來const selectedTime = ref(new Date())創(chuàng)建反應(yīng)式對象)。Subtract 函數(shù)確實返回新Date對象。部分代碼:https://gist.github.com/SirMishaa/efb0efe74c6355aabbcb853f7650c22f我唯一嘗試做的就是執(zhí)行一個返回新日期的函數(shù),并將 my 替換selectedTime為該函數(shù)的返回值。
查看完整描述

1 回答

?
月關(guān)寶盒

TA貢獻1772條經(jīng)驗 獲得超5個贊

我終于解決了這個問題,所以我將描述解決方案。

我創(chuàng)建了一個小示例:https ://codesandbox.io/s/wandering-paper-o952k?file=/src/App.vue

<template>

  <h1>{{ currentTime.toString() }}</h1>

  <button @click="currentTime = addMonths(1, currentTime)">

    Add one month

  </button>

</template>


function addMonths(numberOfMonths, currentTime) {

  const x = currentTime.getDate();

  currentTime.setMonth(currentTime.getMonth() + numberOfMonths);

  console.log(`Function has been called with : ${currentTime}`);

  if (currentTime.getDate() !== x) {

    currentTime.setDate(0);

  }

  return currentTime;

}

Vue 無法檢測到更改,因為我返回一個具有相同引用(與舊對象)的對象。因此,解決方案是創(chuàng)建一個新對象,以便檢測到更改。


相反return currentTime;,做這樣的事情return new Date(currentTime)會起作用。


如果我能幫忙的話我很高興。我已經(jīng)尋找解決方案很長時間了


查看完整回答
反對 回復 2023-09-28
  • 1 回答
  • 0 關(guān)注
  • 217 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

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