最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to get cookie in nuxtServerInit()? - Stack Overflow

programmeradmin5浏览0评论

I am using vue-cookie package which lets me to set and get cookies with ease. What I want is to get this cookie in nuxtServerInit():

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

But, I always get $nuxt is not defined error. Please help!

I am using vue-cookie package which lets me to set and get cookies with ease. What I want is to get this cookie in nuxtServerInit():

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

But, I always get $nuxt is not defined error. Please help!

Share Improve this question asked Oct 7, 2018 at 16:41 AxelAxel 5,16118 gold badges76 silver badges150 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 9

vue cookie is a wrapper around tiny-cookie . Tiny cookie is for browser. So it wont work on server e.g. in nuxtServerInit

In nuxtServerInit you should get cookies from req.cookies

async nuxtServerInit(_, { req }) {
   console.log(req.headers.cookie)
}
发布评论

评论列表(0)

  1. 暂无评论