1 回答

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)尋找解決方案很長時間了
添加回答
舉報