子组件不能响应store的数据的修改

图片描述

父组件是一个对象数组,子组件也是对象数据。点击按钮后,对象上的expand变为true或者false,控制界面的折叠和展开。
父组件都没有问题,子组件expand一直为false。
父组件代码:
。。。

            <q-tr v-show="props.row.expand" :props="props">
                <q-td colspan="100%" style="padding: 7px;">
                    <div v-if="props.row.gauges[0]">{{props.row.gauges[0].expand}}</div>
                    <plant-gauges :profile="props.row" :gauges="props.row.gauges"></plant-gauges>
                </q-td>
            </q-tr>
        </template>
        
        

子组件:点击展开,变量和界面均没有响应。
import plantRanges from './plantRanges.vue'
export default {

name: 'plant-gauges',
props: {
    profile: {
        type: Object,
        default () {
            return {}
        }
    },
    gauges: {
        type: Array,
        default () {
            return []
        }
    }
},   



        gaugeExpand (profileID, stype) {
        this.$store.dispatch('plants/gaugeExpand', { profileID: profileID, stype: stype })
    },        
    
    
    
    store mutation代码:
    export function gaugeExpand (state, Obj) {
var index = state.plantProfiles.findIndex((profile) => (profile._id === Obj.profileID))
if (index > -1) {
    let profile = state.plantProfiles[index]
    var gaugeIndex = profile.gauges.findIndex((gaugeObj) => (gaugeObj.stype === Obj.stype))
    if (gaugeIndex > -1) {
        var gauge = profile.gauges[gaugeIndex]
        gauge.expand = !gauge.expand
        Vue.set(state.plantProfiles, index, profile)
    }
}

}

阅读 2.5k
2 个回答

加多一个prop,
<plant-gauges :profile="props.row" :expand="props.row.expand" :gauges="props.row.gauges"></plant-gauges>
或者在子组件那里watch props.row

搞定了,mutation里面不对,改成这样就好了
export function gaugeExpand (state, Obj) {

var index = state.plantProfiles.findIndex((profile) => (profile._id === Obj.profileID))
if (index > -1) {
    var profile = state.plantProfiles[index]
    var gaugeIndex = profile.gauges.findIndex((gaugeObj) => (gaugeObj.stype === Obj.stype))
    if (gaugeIndex > -1) {
        var gauge = profile.gauges[gaugeIndex]
        gauge.expand = !gauge.expand
        Vue.set(profile.gauges, gaugeIndex, gauge)   // 和界面一致
    }
}

}

感谢liny,应该也可以

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进