【问题标题】:nuxt get username after hit this.$auth.fetchUser()nuxt 在点击 this.$auth.fetchUser() 后获取用户名
【发布时间】:2019-11-13 12:15:20
【问题描述】:

你好,我试图在 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

【问题讨论】:

    标签: vue.js vuejs2 nuxt.js


    【解决方案1】:

    我用这个逻辑修复了它

    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`,
              );
            });
        },
    

    【讨论】:

      猜你喜欢
      • 2020-12-15
      • 2023-03-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-12
      相关资源
      最近更新 更多