在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問(wèn)答/HTML/ 為什么VUEX中的數(shù)據(jù)被組件元素綁定后就無(wú)法被監(jiān)聽(tīng)了

為什么VUEX中的數(shù)據(jù)被組件元素綁定后就無(wú)法被監(jiān)聽(tīng)了

這個(gè)問(wèn)題因?yàn)椴檎覇?wèn)題的方向變化,已經(jīng)做了多次修改。問(wèn)題其實(shí)一直都是一個(gè)問(wèn)題,之前的干擾項(xiàng)太多,自己的經(jīng)驗(yàn)也比較淺。
后來(lái)在vue論壇的大神幫助下,問(wèn)題進(jìn)展是,發(fā)現(xiàn)vuex中的數(shù)據(jù),一旦被組件元素綁定,則computed監(jiān)聽(tīng)失效。項(xiàng)目是VUE腳手架生成的。

下面上代碼了

store.js中

const state = {
    banner: []
}

const mutations = {
    GETBANNER(state, res) {
        state.banner = res;
        console.log(res)
    }
}

const actions = {
    getBanner({commit}) {
        axios.get('/ad/index', {
            params: {
                bannerTypeId: 1
            }
        })
            .then(function (res) {
                commit('GETBANNER', res.data.retData.dataList)
            })
            .catch(function (error) {
                console.log(error);
            });
    }
}

const getters = {
    banner: state => state.banner
}

export default new Vuex.Store({
    strict: true,
    state,
    getters,
    mutations,
    actions
})

組件.vue中的JS

export default {
        name: 'banner',
        computed: {
            banner() {
                return this.$store.getters.banner;
            }
        },
        mounted() {
            this.$store.dispatch('getBanner')
        }
    }

組件.vue的HTML

<template>
    <div id="banner">
        <!--<div v-for="(item,index) in banner" :key="index">-->
            <!--<div>{{item}}</div>-->
        <!--</div>-->
    </div>
</template>

此時(shí)在VUE調(diào)試工具中可以看到值

clipboard.png

<template>
    <div id="banner">
        <div v-for="(item,index) in banner" :key="index">
           <div>{{item}}</div
        </div>
    </div>
</template>

此時(shí)不?+S
組件有數(shù)據(jù),并能正確渲染
刷新頁(yè)面……

clipboard.png

回答
編輯回答
雨蝶

banner這個(gè)數(shù)據(jù)是通過(guò)computed計(jì)算得來(lái)的,同一個(gè)數(shù)據(jù)又是computed又是watch,通常不建議這么做,watch監(jiān)聽(tīng)是監(jiān)聽(tīng)一個(gè)特定的值發(fā)生變化
你在模板里渲染一下banner,就可以了

<li v-for="item in banner.data.retData.dataList">
2018年2月10日 10:13
編輯回答
蝶戀花

先在commit('GETBANNER', res)前面輸出res看看,檢查請(qǐng)求是否有觸發(fā),.vue文件data里面不能直接寫(xiě)成items:banner,因?yàn)槟愕膇tems是在watch里面才賦值,如果你的數(shù)據(jù)是數(shù)組可以寫(xiě)成items:[]

2017年8月17日 22:03