nuxt点击后获得用户名。$ auth.fetchUser()

问题描述 投票:0回答:1

你好,我试图在fetchUser方法后获取用户名,但未定义

我正在这样尝试

   async loginUser() {
      this.$auth.setUserToken(`${this.token}`);
      this.$auth.setStrategy('local');
      await this.$auth
        .fetchUser()
        .then(() => {
          console.log(this.$auth.user); //<- this getting false i want user details
        })
        .catch(e => {
          // console.log(e)
          this.$auth.logout();
          return this.$router.push(
            `/${
              this.$route.query.origin ? this.$route.query.origin : 'register'
            }?error=1`,
          );
        });
    },

我在提交Auth\SET之后的状态enter image description here

vue.js vuejs2 nuxt.js nuxt
1个回答
0
投票

我使用此逻辑对其进行了修复

async loginUser() {
      this.$auth.setUserToken(`${this.token}`);
      this.$auth.setStrategy('local');
      await this.$auth
        .fetchUser()
        .then(() => {
//Added This for get user details then redirect to username page
          this.$axios.get('/me').then(response => {
            return this.$router.push(`/${response.data.data.username}`);
          });
        })
        .catch(e => {
          // console.log(e)
          this.$auth.logout();
          return this.$router.push(
            `/${
              this.$route.query.origin ? this.$route.query.origin : 'register'
            }?error=1`,
          );
        });
    },
© www.soinside.com 2019 - 2024. All rights reserved.