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

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

vuex數(shù)據(jù)更新的問題

vuex數(shù)據(jù)更新的問題

侃侃無極 2018-10-03 17:22:46
<script>     export default {            name: "index",         data() {                     return {                              products: this.$store.state.app.products             }         },         mounted() {                     if (this.products.length <= 0)                    this.$store.dispatch('getProducts');         }     }</script><template>     <ul>         <li v-for="(product,index) of productData ">{{prduct.title}}</li>     </ul> </template>在vuex中的actions中異步獲取數(shù)據(jù)賦值給 products但是這樣做第一次加載時不會有數(shù)據(jù)的 只有在路由切換后才有數(shù)據(jù)有什么辦法讓vuex中賦值之后這邊組件中的數(shù)據(jù)及時更新???
查看完整描述

2 回答

?
滄海一幻覺

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

products不要放在data里定義,應該放在computed計算屬性里定義:

<script>
    export default {  
          name: "index",        
          computed: {
            poducts() {            
                return this.$store.state.app.products
            }
        },
        mounted() {        
            if (this.products.length <= 0) this.$store.dispatch('getProducts');
        }
    }</script>

錯誤原因: 
你先給data中的products賦值為this.$store.state.app.products然后vuex異步獲取數(shù)據(jù)給state.app.products。
此時vuex中的products得到數(shù)據(jù)后更新,但是data不會響應式的根據(jù)vuex中的products的改變來改變,所以data里的products沒有數(shù)據(jù)。
切換路由后,你這個組件沒有進行緩存,它在切換其他路由時已經(jīng)被銷毀,切換回來時重新渲染,取得vuex中的products。
此時vuex中products已經(jīng)有了值,所以data里的products能得到數(shù)據(jù)。
下面是vuex文檔中有提到得到state里值的方式最好是寫在計算屬性里面
vuex文檔


查看完整回答
1 反對 回復 2018-10-08
?
嚕嚕噠

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

沒看懂你問題 你是在組件中獲取不到store中更新后的products數(shù)據(jù)還是組件更新后 store獲取不到products數(shù)據(jù)
一般都是利用監(jiān)聽來監(jiān)聽store中數(shù)據(jù)變化 希望對你有幫助 下次問題在描述具體點
`<script>

export default {
    name: "index", 
    data() {  
          return {
            products: this.$store.state.app.products//這里你賦值給products 模板循環(huán)的是 productData 
        }
    },
    mounted() {     
        //這里是每次進入這個組件都會執(zhí)行這里的代碼
        //你在這里從新賦值給store中getProducts你不需要參數(shù)嗎
        if (this.products.length <= 0) 
            this.$store.dispatch('getProducts');
    }
}

</script>`


查看完整回答
反對 回復 2018-10-08
  • 2 回答
  • 0 關注
  • 2846 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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