【问题标题】:Vue mixin using Vuex for data is not causing components to update使用 Vuex 获取数据的 Vue mixin 不会导致组件更新
【发布时间】:2021-06-17 15:10:50
【问题描述】:

我有一个使用 Vuex 商店的 vue mixin:

const rolesMixin = {
  data: function () {
    const user = store.state.authentication.currentUser;
    return {
      isAdmin: user === null ? false : user.admin,
    };
  }
};

Vue.mixin(rolesMixin);

当我在组件中使用isAdmin 时,它不会在状态更改时重新渲染。必须有更好的方法来使用 mixin 渲染反应组件,有人可以指出正确的方向吗?

用法:

<b-dropdown-item to="/admin/users" v-if="isAdmin">
User
</b-dropdown-item>

【问题讨论】:

    标签: vue.js vuejs2 vue-component vuex


    【解决方案1】:

    非常推荐使用计算属性来获取状态变化:

    const rolesMixin = {
      computed:{
        isAdmin(){
               const user = store.state.authentication.currentUser;
    
               return user === null ? false : user.admin;
    
        }
      }
    };
    
    Vue.mixin(rolesMixin);
    

    【讨论】:

      猜你喜欢
      • 2019-08-13
      • 2019-12-23
      • 2018-06-14
      • 2019-07-30
      • 2019-08-06
      • 2019-09-10
      • 1970-01-01
      • 2020-09-18
      • 1970-01-01
      相关资源
      最近更新 更多