【问题标题】:How to get cookie in nuxtServerInit()?如何在 nuxtServerInit() 中获取 cookie?
【发布时间】:2019-03-12 10:33:24
【问题描述】:

我正在使用vue-cookie 包,它可以让我轻松设置和获取cookie。我想要的是在nuxtServerInit()

async nuxtServerInit() {
   const res = await this.$axios.post('/me', {}, {
       headers: {
          'Authorization': 'Bearer ' + $nuxt.$cookie.get('token')
       }
   })
}

但是,我总是收到$nuxt is not defined 错误。请帮忙!

【问题讨论】:

    标签: javascript vue.js async-await nuxt.js server-side-rendering


    【解决方案1】:

    vue cookie 是 tiny-cookie 的包装器。 Tiny cookie 用于浏览器。所以它不会在服务器上工作,例如在 nuxtServerInit 中

    在 nuxtServerInit 中,您应该从 req.cookies 获取 cookie

    async nuxtServerInit(_, { req }) {
       console.log(req.headers.cookie)
    }
    

    【讨论】:

    • 对我来说,req 是未定义的。我无法从 nuxtServerInit 中读取 cookie
    • @JohanVanWambeke 您在什么模式下使用 nuxt?如果您使用带有nuxt generate 的静态模式,则不会定义req
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-09-12
    • 1970-01-01
    • 2014-09-18
    • 1970-01-01
    • 1970-01-01
    • 2017-12-02
    • 2021-12-31
    相关资源
    最近更新 更多