小编Joh*_*ris的帖子

如何在 axios 插件中使用 Nuxt $auth (How to add Token to all axios requests)

我希望在我的 Nuxt 项目中使用 $auth,特别是在 axios 插件中。

这是我的代码:

插件/api.js

export default function ({ $axios }, inject) {
  const api = $axios.create({
    headers: {
      common: {
        Accept: 'text/plain, */*',
      },
    },
  })

  // Set baseURL to something different
  api.setBaseURL('http://localhost:4100/')

  // Inject to context as $api
  inject('api', api)
}
Run Code Online (Sandbox Code Playgroud)

现在,当我尝试使用 @nuxtjs/auth-next 包中的 $auth 时,问题就出现了。

正如文档中所述:

该模块全局注入 $auth 实例,这意味着您可以使用 this.$auth 在任何地方访问它。对于插件、asyncData、fetch、nuxtServerInit 和 Middleware,您可以从 context.$auth 访问它。

我尝试了以下方法:

  1. 这会导致 $auth 未定义

    导出默认函数 ({ $axios, $auth }, 注入) {

  2. 这个是附近的

    export default function ({ $axios, app },ject) …

authentication plugins axios nuxt.js

5
推荐指数
1
解决办法
4769
查看次数

标签 统计

authentication ×1

axios ×1

nuxt.js ×1

plugins ×1