admin管理员组

文章数量:1398791

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)
}

本文标签: javascriptHow to get cookie in nuxtServerInit()Stack Overflow