【发布时间】:2020-06-25 07:54:07
【问题描述】:
我正在使用 NuxtJS fetch() 挂钩从 REST 服务获取异步数据。以下是请求
async fetch() {
let response = await this.$axios.$get(`http://localhost:8080/app-server/v1/post/id/${this.$route.params.id}`);
}
我还需要发送一个可选的查询参数uid,这是用户登录时的用户ID。用户ID存储在Vuex状态,可以在客户端访问,如下所示
this.$store.state.user.userDetails.id
但是当页面重新加载并在服务器端执行 fetch() 时,这将返回 null。我也在寻找一种在服务器端访问此状态的方法。我尝试使用 Nuxt 上下文 this.$nuxt.context 如下所示,但在服务器端运行时它也返回 null。
this.$nuxt.context.store.state.user.userDetails.id
关于如何在服务器端访问 Vuex 状态的任何建议。谢谢
【问题讨论】:
-
您的商店在页面刷新后被清除,因此没有数据。
-
@Ifaruki 用户信息也保存在 cookie 中。所以在页面刷新后从那里检索它。
-
好的,你在哪里检索它/如何?
标签: vue.js vuejs2 nuxt.js server-side-rendering nuxtjs