javascript - 为什么我得到默认的 vuex 存储值而不是计算属性的更新值?
问题描述
我有一个initial
(起始)页面,用户可以在其中按下按钮登录。另外,在该页面中,我想知道用户是否已经登录,因此我正在使用mapState
它来访问isLoggedIn
我商店的状态变量。
在代码中:
// Initial.vue
<template>
<v-container
app
fluid
>
<v-parallax
src="https://cdn.vuetifyjs.com/images/backgrounds/vbanner.jpg"
height="1000"
>
<v-layout
row
wrap
>
<!-- LOGIN-->
<v-toolbar
flat
light
dense
color="transparent"
>
<v-spacer></v-spacer>
<v-toolbar-items>
<v-btn
medium
color="lime lighten-2"
@click="login"
class="font-weight-bold title text-uppercase"
>
LOGIN
</v-btn>
</v-toolbar-items>
</v-toolbar>
<v-layout
align-center
column
>
<h1 class="display-2 font-weight-thin mb-3 text-uppercase lime--text lighten-2" >My page</h1>
<h4 class="subheading">{{isLoggedIn}}</h4>
</v-layout>
</v-layout>
</v-parallax>
</v-container>
</template>
<script>
import VContainer from "vuetify/lib/components/VGrid/VContainer";
import VLayout from "vuetify/lib/components/VGrid/VLayout";
import VBtn from "vuetify/lib/components/VBtn/VBtn";
import VToolbar from "vuetify/lib/components/VToolbar/VToolbar";
import VParallax from "vuetify/lib/components/VParallax/VParallax";
import { mapState } from 'vuex';
export default {
name: "Initial",
components: {
VContainer,
VLayout,
VBtn,
VToolbar,
VParallax
},
//Upon returning to this page isLoggedIn is false eventhough isLoggedIn has been updated to store
computed: mapState({
isLoggedIn: state => state.isLoggedIn
}),
methods: {
login() {
this.$auth.login();
},
}
}
</script>
<style scoped>
</style>
用户登录后(通过auth0
),他会被重定向到注册表单。表单提交后,数据被发送到后端,我正在调用一个REGISTER
操作来更新存储状态,然后重定向到/home
. 这样做的代码是:
//CompleteSignup.vue
this.$store.dispatch(REGISTER,registerFormData)
.then(() => this.$router.push('/home'));
我的商店是这样的:
//index.js
import Vue from 'vue';
import Vuex from 'vuex';
import {APIService} from '@/common/api.service';
import {REGISTER} from "./actions.type";
import {SET_ERROR, SET_AUTH} from "./mutations.type";
Vue.use(Vuex);
export default new Vuex.Store({
strict: process.env.NODE_ENV !== 'production',
state: {
errors: null,
user: {},
isLoggedIn: false
},
getters: {
currentUser(state) {
return state.user;
},
},
actions: {
[REGISTER]({commit}, registerData) {
return new Promise((resolve, reject) => {
APIService.create(registerData)
.then(({data}) => {
commit(SET_AUTH, data);
resolve(data);
})
.catch(({response}) => {
commit(SET_ERROR, response.data.errors);
reject(response);
});
});
}
},
mutations: {
[SET_ERROR](state, error) {
state.error = error;
},
[SET_AUTH](state, user) {
state.isLoggedIn = true;
state.user = user.user_id;
state.errors = {};
}
}
})
当我在里面时,/home
我可以从 Vue devtools 中看到我的商店变量已正确更新:
当我导航回/initial
页面时出现问题。我希望当用户返回该视图时,该computed
值isLoggedIn
将返回true
;但相反,我得到了默认的存储值(false
)。
为什么不mapState
返回store 变量true
isLoggedIn
?
另外-尽管我不确定它是否相关-我注意到在这种情况下,vue devtools
再次Base state
将附加屏幕截图的第一个显示为活动屏幕截图(也具有相同的时间戳)。就好像返回动作重新触发了原始状态,而不是创建一个新状态。
解决方案
推荐阅读
- java - Java的未来,诺言还是?
- reactjs - 如何根据reactjs中的url在reactjs中进行Api调用?
- python - BeautifulSoup 从 find_all 的结果中找到 url
- api - WSO2 API Manager 认证考试
- javascript - 如果用户之前访问过网站,如何触发特定操作?
- aws-lambda - SubnetIds 和 SecurityGroupIds 的 Lambda 模板失败
- java - I am trying to reverse my recyclerview items but neither setReverseLayout nor Collection.reverse() works
- python - 从长度不均匀的字典创建熊猫数据框
- java - 有没有办法在 Android Studio 中使用代码访问“隐藏我的电话号码”(来电显示)功能?
- fullscreen - Angular Material - 一些脚本停止以全屏模式运行