【问题标题】:Double requests before open page打开页面前的双重请求
【发布时间】: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;
    },
}

在 chrome devtools 的网络中,我有这个:

它表明我的应用程序在第一次请求之后发送了相同的请求,有人知道为什么会这样吗?

感谢您的帮助!

【问题讨论】:

    标签: javascript typescript vue.js vuex


    【解决方案1】:

    根据您的代码,您调用了两个函数,例如从服务器获取数据的 authenticated() 和 fetchProjects()

    【讨论】:

      猜你喜欢
      • 2014-09-16
      • 1970-01-01
      • 2012-04-11
      • 2014-12-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-06
      • 1970-01-01
      • 2013-05-12
      相关资源
      最近更新 更多