如何在 nuxtServerInit() 中获取 cookie?

How to get cookie in nuxtServerInit()?

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

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

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

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

在 nuxtServerInit 中,您应该从 req.cookies

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