计算属性不在不同选项卡中更新UI

时间:2017-03-30 09:57:26

标签: vue.js vuejs2 vuex

  • 我使用 vuex 进行状态管理并使用身份验证用户 的火力
  • 我使用vuex-persisted state将状态保存在Cookie中

在我的vuex商店中,我管理 userdata (用户名,登入状态),如下所示

my store.js

/all imports here
export const store = new Vuex.Store({
   state : {
    user: {
        loggedIn: false,
        userName: 'Guest'
    }
},

getters : {
    g_user: state => {
        return state.user;
    }
},

mutations : {
    m_logInUser: (state, userName) => {
        state.user.loggedIn = true;
        state.user.userName = userName;

    },
    m_loggedOut: (state) => {
        state.user.loggedIn = false;
        state.user.userName = 'Guest';
    }
},

actions : {
    a_logInUser: ({state, commit}, userInput) => {
        //call to the API and on success commit m_logInUser mutation
    },
    a_loggedOut: ({commit}) => {
        //call to the API and on success commit m_loggedOut mutation
    }
},
plugins: [
     createPersistedState({
         paths: ['authStore.user'],
         getState: (key) => Cookie.getJSON(key), 
         setState: (key, state) => Cookie.set(key, state, { expires: 3, secure: false })
     })
]
});

现在我面临的问题

  • 当我在两个不同的选项卡中打开应用程序并在第一个选项卡中登录用户时,用户登录现在隐藏登录按钮并显示注销按钮并显示用户名

  • 但是在第二个标签中它仍然显示登录按钮,但是当我为用户提供console.log时,它显示已登录并且还显示用户名

这是我的header.vue组件

<template>
  <nav class="navbar navbar-default">
<div class="container-fluid">
  <div class="navbar-header">
    <router-link to="/" tag="a" class="navbar-brand">Brand</router-link>
 </div>
  <!-- Collect the nav links, forms, and other content for toggling -->
  <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
    <ul class="nav navbar-nav">
        <li @click="testmethod"><a>cuser</a></li>
        <router-link to="/statuses" active-class="active" tag="li"><a>Status</a></router-link>
        <router-link to="/users" active-class="active" tag="li"><a>Users</a></router-link>
    </ul>
    <ul class="nav navbar-nav navbar-right" v-if="!g_user.loggedIn">
      <router-link to="/signup" active-class="active" tag="li"><a>Signup</a></router-link>
      <router-link :to="{name: 'login'}" active-class="active" tag="li"><a>Login</a></router-link>
    </ul>
    <ul class="nav navbar-nav navbar-right" v-else>
        <router-link to="/post" tag="li"><a><button class="btn btn-info">POST</button></a></router-link>
        <router-link to="/current" active-class="active" tag="li"><a>{{ g_user.userName }}</a></router-link>
        <li @click="logOut"><a>Log out</a></li>
    </ul>
  </div><!-- /.navbar-collapse -->
</div><!-- /.container-fluid -->
</nav>
</template>

<script>
import { mapGetters } from 'vuex'
export default{
    methods: {
        logOut(){
            this.$store.dispatch('a_loggedOut');
        },
        testmethod(){
            var user = this.$firebase.auth().currentUser;

        console.log(user);
        console.log(user.email);
        }

    },
    computed: {
        ...mapGetters([
            'g_user'
        ])
    }
}
</script>

1 个答案:

答案 0 :(得分:1)

你可以watch你的Vuex状态:

mounted() {
  this.$store.watch(
    function(state) {
      return state.g_user.loggedIn;
    },
    function(value) {
      if (value === true) {
        console.log('user is logged in');
        // do your logic here, setup loggedin behavior hooks. For example call login() function which should first check if the user is already logged in
      }
    },
    {
      immediate: true
    }
  );
},