nuxtjs - 如何从 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 端点的任何方法?
解决方案
我通过使用 auth 和 vuex 方法解决了它。我不需要拦截电话。只需使用
this.$auth.user
或者
this.$store.state.auth.user;
学习 Nuxt/Vuejs 很有趣。
推荐阅读
- python - 显示从 500 个随机数字池中选择 x 数字的次数
- javascript - 您可以将EventListener 添加到另一个元素中的元素吗
- c++ - 队列中的 Delete_at 无法正常工作
- android - 通知通道的重复振动模式
- python - 比较字典中的列表并获得获胜的关键
- c# - EF 核心导航属性为空
- javascript - MomentJS - 获取日期范围给出一个月中的特定日期
- kubernetes - Airflow Kubernetes Executor pod 进入“NotReady”状态而不是被删除
- git - 使用 git ls-remote 显示除候选版本之外的所有标签
- ruby-on-rails - 无法加载此类文件 -- pty (LoadError) 开发环境设置