Vue.js 直接调用子组件会导致vuex中出现空值
我将VueJS与Web包、vuex和vue路由器一起使用。我试图通过使用子组件中的计算功能来检查用户是否已注册。计算功能尝试查看my vuex中的用户属性。如果我通过这个URL打开我的Web应用程序,这很好http://localhost:8080. 只有通过以下URL直接调用子组件时,才会出现问题:http://localhost:8080/meetup/id. 出现错误:Vue.js 直接调用子组件会导致vuex中出现空值,vue.js,webpack,vue-router,Vue.js,Webpack,Vue Router,我将VueJS与Web包、vuex和vue路由器一起使用。我试图通过使用子组件中的计算功能来检查用户是否已注册。计算功能尝试查看my vuex中的用户属性。如果我通过这个URL打开我的Web应用程序,这很好http://localhost:8080. 只有通过以下URL直接调用子组件时,才会出现问题:http://localhost:8080/meetup/id. 出现错误:TypeError:无法读取null的属性“registeredMeetups”,因为此时用户属性甚至不存在 子组件计算代
TypeError:无法读取null的属性“registeredMeetups”,因为此时用户属性甚至不存在
子组件计算代码:
computed: {
userIsRegistered() {
console.log(this.$store.getters.getUser)
if (this.$store.getters.getUser.registeredMeetups.findIndex(meetupId => {return meetupId === this.meetupId;}) >= 0) {
return true;
} else { return false; }
}
}
用户属性在Vue实例的main.js中设置:
new Vue({
router,
store,
vuetify,
render: function (h) { return h(App) },
created () {
this.initialFirebaseConfig();
this.isUserAuthSignIn();
this.$store.dispatch('loadMeetups');
},
methods: {
initialFirebaseConfig() {
const firebaseConfig = {
...
};
// Initialize Firebase
firebase.initializeApp(firebaseConfig);
},
isUserAuthSignIn() {
firebase.auth().onAuthStateChanged((user) => {
if(user) {
this.$store.dispatch('autoSignIn', user)
}
});
}
}
vue路由器代码:
{
path: "/meetup/:id",
name: "Meetup",
props: true,
component: () => import(/* webpackChunkName: "meetup" */ '../components/Meetup/Meetup.vue')
},
目标:子组件的计算功能应该在main.js的vue实例功能之后运行。这是否回答了您的问题?谢谢,它工作了,因为exta if条件和变量的拆分。
const user = this.$store.getters.getUser
if (user && user.registeredMeetups.findIndex(meetupId => {return meetupId === this.meetupId;}) >= 0) {
return true;
} else {
return false;
}