在vuex商店我有此突變,其從一個組件接收MSG和是顯示/隱藏提示消息在另一部件(成功登錄後贊You are logged in
propmpt):如何在vuex store中更改值時更新組件中的狀態?
setPromptMsg: function (state, msg) {
state.showPromptMsg = true;
state.promptMsg = msg;
function sleep (time) {
return new Promise((resolve) => setTimeout(resolve, time));
}
sleep(3000).then(() => {
store.showPromptMsg = false;
state.promptMsg = '';
console.log('show message set to false');
});
},
在compoenet,我接收showPromptMsg
從商店作爲計算的屬性:
computed: {
showPromptMsg() {
return this.$store.state.showPromptMsg;
},
promptMsg() {
return this.$store.state.promptMsg;
}
}
的顯示/隱藏在模板提示消息:
<div v-show="showPromptMsg">
<div class="text-center" >
<strong> {{promptMsg}} </strong>
</div>
</div>
問題是,當提示符超時時,即showPromptMsg
在存儲中設置爲false時,更改不會反映到組件中,因此通知框不會消失。
我想知道什麼是地道的方式來解決這個問題?