【发布时间】:2020-05-02 17:51:18
【问题描述】:
我有 VueJS 单页应用程序 (SPA),当用户在我的应用程序中成功注册时,它会将他们重定向到主页。此主页显示一个栏(警报),要求用户确认他们在注册期间提供的电子邮件。 注意:我们会一直显示这个栏,直到他们确认!
目前,当用户确认电子邮件时,它会在新选项卡中打开确认页面,显示 Email have been successfully confirmed! 并且该栏消失,但当用户转到原始选项卡(主页)时,该栏仍会显示。
有什么方法可以在后台监听用户状态的变化 (user.isVerified) 并在没有用户参与的情况下隐藏栏(刷新页面/点击任何链接)?
为了获取用户数据,我使用 API 调用:
getUser({ commit }) {
return new Promise((resolve, reject) => {
axios
.get('http://localhost:8000/api/user')
.then(response => {
commit('setData', response.data.data);
resolve(response);
})
.catch(errors => {
reject(errors.response);
})
})
}
JSON 响应:
{
"data": {
"id": 2,
"name": "John Doe",
"email": "john@email.com",
"isVerified": true,
"createdAt": "2020-04-17T13:17:07.000000Z",
"updatedAt": "2020-04-26T23:15:24.000000Z"
}
}
Vue 页面:
<template>
<v-content>
<v-alert
v-if="user.isVerified === false"
tile
dense
color="warning"
dark
border="left"
>
<div class="text-center">
<span>
Hey {{ user.name }}! We need you to confirm your email address.
</span>
</div>
</v-alert>
<v-content>
<router-view></router-view>
</v-content>
</v-content>
</template>
【问题讨论】: