首页 > 解决方案 > 如何从 this.$auth.fetchUser() 存储用户?

问题描述

我有一个验证良好的 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

解决方案


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

this.$auth.user

或者

this.$store.state.auth.user;

学习 Nuxt/Vuejs 很有趣。


推荐阅读