vue如何双向绑定computed计算属性

业务逻辑是:
进来界面有个弹窗,是否展示为isShowDialog,它的初始值是跟着store数据变化

代码如下:

<template>
    <diglog v-if="isShowDialog" @close="isShowDialog = false"/>
</template>
import {mapState} from 'vuex'
export default {
    computed: {
        ...mapState({
            nameAuth: (state) => state.user.nameAuth
        }),
        isShowDialog(){
            return this.nameAuth != 1
        }
    }
}

如果关闭弹窗,就会导致报错Computed property "nameAuth" was assigned to but it has no setter.

阅读 4.9k
2 个回答

这属于vue基础啊...

export default {
    computed: {
       nameAuth:{
           get(){
               return this.$store.state.user.nameAuth
           },
           set(v){
               this.$store.commit("user/setNameAuth",v);
           }
       },
        isShowDialog(){
            return this.nameAuth != 1
        }
    }
}

vuex

mutations:{
    setNameAuth(state,value){
        state.nameAuth=value;
    }
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题