我有一家vuexstore .在vuexstore 中更改状态首选项.我想重播DOM.我希望每次vuex存储中的状态首选项更改时都调用checkValue方法.

指数html

<div id="app">
    <my-component></my-component>
    <my-other-component></my-other-component>
</div>

vue已初始化,store 也在此处导入

我的_组件.js

Vue.component('my-component',require('./我的成分.vue'));
import store from "./百货store js"
Vue.component('my-other-component',require('./肌肉治疗成分.vue'));
import store from "./百货store js"

new Vue({
    el : "#app",
    data : {},
    store,
    method : {},
})

当存储区中的状态首选项发生变化时,需要更改DOM的组件

我的成分.vue

<template>
    <div v-for="object in objects" v-if="checkValue(object)">
        <p>hello</p>
    </div>
</template>


<script>
    methods : {
        checkValue : function(object) {
            if(this.preference) {
                // perform some logic on preference
                // logic results true or false
                // return the result
            }
        }
    },

    computed : {
        preference : function() {
            return this.$store.getters.getPreference;
        }
    }


</script>

Vuex存储文件

百货store js

const store = new Vuex.Store({
state : {
    preferenceList : {components : {}},
},
getters : {
    getPreference : state => {
        return state.preferenceList;
    }
},
mutations : {
    setPreference : (state, payload) {
        state.preference['component'] = {object_id : payload.object_id}
    }
}

单击li元素时更新vuex存储的组件.

肌肉治疗成分.vue

<div>
    <li v-for="component in components" @click="componentClicked(object)">
    </li>
</div>


<script type="text/javascript">
    methods : {
        componentClicked : function(object) {
            let payload = {};
            payload.object_id = object.id;
            this.$store.commit('setPreference', payload);
        }
    }
</script>

推荐答案

方法不是被动的,这意味着它们不会跟踪更改并在某些更改时重新运行.这就是你计算的结果.

这意味着您需要使用computed来计算所需的内容,但computed不接受参数,您需要对象,因此解决方案是创建另一个组件,该组件接受对象作为属性,然后在那里执行逻辑:

MyOtherComponent.vue:

<template>
    <div v-if="checkValue">
        <p>hello</p>
    </div>
</template>


<script>
    props:['object','preference']
    computed : {
        checkValue : function() {
             if(this.preference) {
               // perform some logic on preference
               // logic results true or false
               return true
             }

             return false
        }
    }


</script>

然后在原始组件中:

<template>
    <my-other-component v-for="object in objects" :object="object" :preference="preference">
        <p>hello</p>
    </my-other-component>
</template>

Vue.js相关问答推荐

如何在Vue3中使用vuechartjs 5.2从父组件更新图表

在 vue.js 中添加新类时样式不适用

如何使用 vue 或 javascript 向服务器发送密码?

如何使用 vue.js 获取本地 html 文件?

在 vuejs 中显示两个组件,但只显示一个,为什么?

在 SASS 中使用 Vuetify 类

在基于 TypeScript 的 Vue 中将 vuex 状态和Mutations绑定到复选框组件属性

自定义props类型

如何在不实例化根实例的情况下使用 vue 组件?

如何将参数传递给使用 ...mapActions(...) 映射的函数?

如何告诉 Vue 应用使用 Firebase 模拟器?

从 Axios 响应中解析 XML,推送到 Vue 数据数组

这是 v-on:change 的正确用法吗?

从 vue.js 中的 store 获得的计算(computed)属性的设置器

Vue.js 的 $emit 和 $dispatch 有什么区别?

laravel vuejs/axios put request Formdata 为空

如何在 vue.js 中进行嵌入?

带有 Firebase 云消息传递的 Vue pwa 无法正常工作

为什么 router.currentRoute.path 没有react?

如何在 VeeValidate 中自定义错误信息(字段名称)?