【问题标题】:How to solve the error "Redirected when going from ' / ' to '/dashboard' via a navigation guard"?如何解决“通过导航守卫从'/'到'/dashboard'时重定向”错误?
【发布时间】:2021-08-24 10:00:06
【问题描述】:

当我使用用户登录时,它会按预期将我重定向到仪表板。一旦我注销并尝试再次登录(即使使用另一个用户,并且没有刷新页面),它也会在控制台中返回此错误:

如果经过身份验证,我只想在仪表板中重定向用户,即使页面没有刷新,因为我确实注意到如果我刷新页面,我可以毫无问题地登录。

如果可以,请帮助我。下面是一些代码:

登录方式

methods: {
    ...mapActions({
    attempt: "auth/attempt",
    }),

    submit(credentials) {
      axios
        .post("http://127.0.0.1:8000/api/login", credentials)
        .then((res) => {
          // console.log(res.data);
          if (res.data.success) {
            
            this.attempt(res.data.token)
          }

          if (res.data.errors) {
            this.loginErrors = res.data.errors;
          } else {
            this.$router.push({ name: 'dashboard' })
          }

          
        })
        .catch((err) => {
          if (
            err.name !== "NavigationDuplicated" &&
            !err.message.includes(
              "Avoided redundant navigation to current location"
            )
          ) {
            
            console.log(err);
          }
        });
    },
  },

路由器中的仪表板路径

        {
            path: '/dashboard',
            name: 'dashboard',
            component: DashboardComponent,
            beforeEnter: (to, from, next) => {
                if (!store.getters['auth/authenticated']) {
                    return next({
                        name: 'home'
                    })
                }
                next()
            }
        },

在 vuex 商店中尝试操作

async attempt({ commit, state }, token) {
            if (token) {
                commit('SET_TOKEN', token)
            }

            // se non c'è
            if(!state.token) {
                return
            }
            
            try {
                await axios.get('http://127.0.0.1:8000/api/user')
                    .then(res => {
                        commit('SET_USER', res.data)
                    })
            } catch (e) {
                commit('SET_TOKEN', null)
                commit('SET_USER', null)
            }
        },

来自 vuex 的其他人

namespaced: true,
    state: {
        token: null,
        form: null,
    },

    getters: {
        authenticated(state) {
            return state.token && state.form
        },

        user(state) {
            return state.form
        },
    },

    mutations: {
        SET_TOKEN(state, token) {
            state.token = token
        },

        SET_USER(state, data) {
            state.form = data
        },

    },

【问题讨论】:

    标签: vue.js vuex vue-router


    【解决方案1】:

    更新:应该等待对attempt() 的调用,否则this.$router.push({ name: 'dashboard' })(因此/dashboard 路由上的保护函数)将在调用/api/user 之前被调用API 已完成:

        submit(credentials) {
          axios
            .post("http://127.0.0.1:8000/api/login", credentials)
            .then(async (res) => {
              // console.log(res.data);
              if (res.data.success) {
                await this.attempt(res.data.token)
              }
    
              if (res.data.errors) {
                this.loginErrors = res.data.errors;
              } else {
                this.$router.push({ name: 'dashboard' })
              }
            })
            .catch((err) => {
              if (
                err.name !== "NavigationDuplicated" &&
                !err.message.includes(
                  "Avoided redundant navigation to current location"
                )
              ) {
                
                console.log(err);
              }
            });
        },
    

    next is a function that should be called exactly once返回)。
    尝试将路由器中的代码更改为:

            {
                path: '/dashboard',
                name: 'dashboard',
                component: DashboardComponent,
                beforeEnter: (to, from, next) => {
                    if (!store.getters['auth/authenticated']) {
                        next({ name: 'home' })
                    } else {
                        next()
                    }
                }
            },
    

    【讨论】:

    • 我以前试过这种方法,但是没用,控制台报同样的错误信息
    • 如果我使用 location.href = '/dasboard' 它可以工作,但显然它会重新加载页面,而不是像 SPA 一样
    • 我还注意到没有等待对this.attempt(res.data.token) 的调用。你能试着解决这个问题吗?
    • 你的意思是可以调用 this.$store.dispatch('attempt', res.data.token) 代替?
    • 但是在网络中对 api/user 的调用是成功的,所以应该已经触发了尝试方法
    猜你喜欢
    • 1970-01-01
    • 2021-01-13
    • 1970-01-01
    • 2021-07-01
    • 1970-01-01
    • 2021-05-06
    • 2020-11-23
    • 2021-02-03
    • 1970-01-01
    相关资源
    最近更新 更多