vue.js - Nuxtjs 组件不会在 v-if 条件更改时重新加载
问题描述
我有一个导航栏组件,其中包含 2 个按钮“注销”和“仪表板”,仅当用户通过身份验证时才会呈现。(使用 v-if)。
但是,当用户单击“注销”按钮时,导航栏不会重新加载,因此按钮会保持可见,直到您重新加载页面。如何设置我的 v-if 条件,以便当 v-if 的值发生变化时,组件(div)也会更新?
我的导航栏
<!-- ... -->
<template slot="end">
<b-navbar-item tag="div">
<div class="buttons">
<a v-if="userToken" class="button is-primary" style='padding:4px; margin-right:0;'>
<b-button size="is-normal"
type="is-primary"
icon-right="close"
outlined
inverted
style="margin-bottom: 0;"
@click="logout">
Logout
</b-button>
</a>
<a v-if="userToken" class="button is-primary" style='padding:4px; margin-left:0;'>
<b-button size="is-normal"
type="is-success"
icon-right="account-circle-outline"
style="margin-bottom: 0;"
@click="goToDashboard">
Dashboard
</b-button>
</a>
<a v-else class="button is-primary">
<b-icon icon="account-circle-outline" size="is-medium"> </b-icon>
</a>
</div>
</b-navbar-item>
</template>
</b-navbar>
</template>
<script>
export default {
data () {
return {
userToken : '',
},
methods: {
getUserToken() {
let token = this.$cookies.get('userToken')
if (token) {
this.userToken = token
this.$router.push('user_dashboard')
}
},
logout() {
this.$cookies.remove('userToken')
this.userToken = ''
}
},
mounted() {
this.getUserToken()
},
// ...
</script>
解决方案
好的,我真的很抱歉,我最终通过进行这些更改解决了这个问题:
methods: {
getUserToken() {
let token = this.$cookies.get('userToken')
if (token) {
this.userToken = token
this.$router.push('user_dashboard')
} else {
this.userToken = ''
}
},
logout() {
this.$cookies.remove('userToken')
this.getUserToken()
}
},
mounted() {
this.getUserToken()
},
推荐阅读
- mysql - 计算行之间的日期时间
- ruby-on-rails - 我面临一个关于 postgresql 的问题
- php - 使用 Laravel,我在通知颤振应用程序时收到以下错误
- kubernetes - 如何在 Kubernetes 部署配置中为主机名起别名
- python - 用 numpy 求解 Gram 矩阵上的线性方程
- android - WebView 不加载 Instagram 登录页面
- sql - ORA-01403: 从 INSERT 触发器中找不到数据
- python - PyCharm 上未解决的参考“rasa_nlu”
- c++ - 了解为什么 Gprof 中没有分析某些函数?
- javascript - 使用 JS 模块运行文件时出现类型错误