【问题标题】:How store user from this.$auth.fetchUser()?如何从 this.$auth.fetchUser() 存储用户?
【发布时间】:2020-12-15 17:20:05
【问题描述】:

我有一个验证良好的 Nuxtjs 应用程序。但我需要用户详细信息才能自动填写表格。我看到应用程序在每次重新加载时都会调用 /user 端点。我想在其回调中插入一个 $store 以将用户数据存储在 $store 中。

computed: {
    getUser () {
      return this.$store.state.user.user;
    }
  },
  methods: {
    setUser (data) {
      this.$store.commit('user/add', data)
    },
}

NUXT 配置:

auth: {
strategies: {
    local: {
        endpoints: {
            login: {
                url: '/auth/login',
                method: 'post',
                propertyName: 'access_token'
            },
            logout: {
                url: '/auth/logout',
                method: 'post'
            },
            user: {
                url: '/auth/user',
                method: 'get',
                propertyName: false
            },
            tokenRequired: true
        }
    }
}

}

是否可以拦截 $auth.fetchUser 或 $auth 用于在每次重新加载时获取 api/user 端点的任何方法?

【问题讨论】:

    标签: nuxtjs


    【解决方案1】:

    我通过使用 auth 和 vuex 方法解决了这个问题。我不需要拦截电话。只需使用

    this.$auth.user
    

    this.$store.state.auth.user;
    

    学习 Nuxt/Vuejs 很有趣。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-03-26
      • 2013-06-27
      • 2012-08-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-28
      相关资源
      最近更新 更多