TLDR:
如果有帮助,我录制了一段视频来解释我的问题。 https://youtu.be/Qf9Q4zIaox8
Navbar组件未在商店更改时更新。
我使用v-if和v-else将我的组件分成两个部分。当用户登录v-if节目并且用户未登录时,v-else会显示。
登录时,服务器进行身份验证,客户端向商店提交变异并设置用户和客户端令牌。设置此项后,它会在名为" userIsLoggedIn"的状态下设置属性。为true - 此属性在navbar组件中计算。由于未知原因,一旦用户登录,Navbar组件就不会更新(尽管isLoggedIn属性为true),除非我刷新页面。
Navbar组件:
<template>
<div class="navbar">
<div class="container">
<div class="navbar-brand">
<h1 class="navbar-item">
<router-link to="/" class="has-text-black">Logo</router-link>
</h1>
</div>
<div class="navbar-menu">
<div class="navbar-end" v-if="isLoggedIn">
<div class="navbar-item">
<router-link to="/play" class="has-text-black">Play</router-link>
</div>
<div class="navbar-item">
{{user.balance.toFixed(2)}}
</div>
<div class="navbar-item has-dropdown is-hoverable">
<a class="navbar-link">
{{user.username}}
</a>
<div class="navbar-dropdown">
<div class="navbar-item">Profile</div>
<div class="navbar-item">Deposit</div>
<div class="navbar-item" @click="logout">Logout</div>
</div>
</div>
<div class="navbar-item">
FAQ
</div>
</div>
<div class="navbar-end" v-else>
<div class="navbar-item">
<router-link to="/login" class="has-text-black">Login</router-link>
</div>
<div class="navbar-item">
<router-link to="/register" class="has-text-black">Register</router-link>
</div>
<div class="navbar-item">FAQ</div>
</div>
</div>
</div> <!-- container -->
</div> <!-- navbar -->
</template>
<script>
import {mapGetters} from "vuex";
export default {
name: "Navbar",
data() {
return {
user: this.$store.state.user,
}
},
computed: {
...mapGetters([
"isLoggedIn",
])
},
methods: {
logout() {
this.$store.dispatch("setToken", null);
this.$store.dispatch("setUser", null);
this.$router.push({
path: "/"
})
}
}
}
</script>
&#13;
Vuex商店:
import Vue from "vue";
import Vuex from "vuex";
import createPersistedState from "vuex-persistedstate"
Vue.use(Vuex);
export default new Vuex.Store({
strict: true,
plugins: [
createPersistedState()
],
state: {
token: null,
user: null,
isUserLoggedIn: false,
},
getters: {
isLoggedIn(state) {
return state.isUserLoggedIn;
}
},
mutations: {
setToken(state, token) {
state.token = token;
if (token) {
state.isUserLoggedIn = true;
} else {
state.isUserLoggedIn = false;
}
},
setUser(state, user) {
state.user = user;
}
},
actions: {
setToken ({commit}, token) {
commit("setToken", token);
},
setUser ({commit}, user) {
commit("setUser", user);
}
}
})
&#13;
登录组件:
<template>
<div class="hero is-large">
<div class="container">
<div class="hero-body">
<div class="field">
<div class="notification is-warning" v-show="error">{{error}}</div>
</div>
<h1 class="title">Login</h1>
<div class="field">
<label for="username" class="name">Username</label>
<input type="text" class="input" name="username" v-model="username">
</div>
<div class="field">
<label for="username" class="name">Password</label>
<input type="password" class="input" name="password" v-model="password">
</div>
<div class="field">
<button class="button is-link"
@click="login"
v-bind:class="{'is-loading': pending}">Login</button>
</div>
</div>
</div>
</div>
</template>
<script>
import AuthenticationService from "../Services/AuthenticationService";
export default {
name: "Login",
data () {
return {
username: "",
password: "",
error: null,
pending: false,
}
},
methods: {
async login () {
try {
// SET LOADING SPINNER
this.pending = true;
// API REQUEST
const response = await AuthenticationService.login({
username: this.username,
password: this.password,
});
console.log(response);
// CLEAR FORM
this.pending = false;
this.username = "";
this.password = "";
this.$store.dispatch("setToken", response.data.token);
this.$store.dispatch("setUser", response.data.user);
this.$router.push({
name: "Landing"
})
} catch (error) {
console.log(error);
}
}
}
}
</script>
&#13;
答案 0 :(得分:0)
我想我发现了什么是错的。当用户登录时你没有将isUserLoggedIn设置为true。当用户成功登录时,进行操作,然后将变量更改为isUserLoggedIn为真。
动作
setUserLogged ({commit}) {
commit("setUserLogged", true);
}
突变
setUserLogged(state, payload) {
state.isUserLoggedIn = true
}
然后在这些行的Login组件中更改:
this.$store.dispatch("setToken", response.data.token);
this.$store.dispatch("setUser", response.data.user);
到此:
this.$store.dispatch("setToken", response.data.token);
this.$store.dispatch("setUser", response.data.user);
this.$store.dispatch("setUserLogged");