【发布时间】:2021-06-11 09:22:10
【问题描述】:
我有一个非常……奇怪的问题,
我在 chrome devtools 的 network 选项卡中看到我的 vue 应用程序将双重请求发送到相同的端点:/
这是我的代码,在路由器部分我有一个函数 beforeEach,当我导航到 /account/projects 时,我从我的 vuex 存储启动调度以从服务器获取数据:
function fetchProjects() {
store.dispatch("getProjects");
store.dispatch("getFavoriteProjects");
}
router.beforeEach((to, from, next) => {
//when authenticated == true, current user must be logged in to view page
if(to.meta.authenticated) {
if(authenticated()) {
//when authenticated -> fetch data about current user to vuex store
if(to.path == "/account/projects" || to.path == "/account") {
fetchProjects();
}
store.dispatch("updateUserData").then(() => {
next();
});
} else {
//when not authenticated -> redirected to login page
next("/auth/login");
}
} else {
next();
}
})
在路线中我有这个:
{
path: "/account/projects",
component: () => import("@/views/account/Projects.vue"),
meta: {
showProgressBar: true,
authenticated: true
},
}
在组件中:
computed: {
projects() {
return this.$store.getters.getProjects;
},
favoriteProjects() {
return this.$store.getters.getFavoriteProjects;
},
}
它表明我的应用程序在第一次请求之后发送了相同的请求,有人知道为什么会这样吗?
感谢您的帮助!
【问题讨论】:
标签: javascript typescript vue.js vuex