如何在 Quasar 中包含 axios 拦截器

发布于 2025-01-14 05:15:13 字数 903 浏览 3 评论 0原文

我目前正在开发一个使用 axios 库的 Quasar 应用程序。用户像往常一样通过令牌进行身份验证,当令牌过期时,必须使用续订令牌进行续订。我需要一个 axios 拦截器来过滤状态 401,以便我可以请求新的令牌并再次发送请求。 拦截器已经编码,现在的问题是我不知道如何让Quasar使用该插件。目前正在生产的应用程序是使用 Vue 2 完成的,它是这样的:

const _axios = axios.create(*config*);
axios.interceptors.request.use(.....);

Plugin.install = function(Vue, options) { .... }
Vue.use(Plugin)
export default Plugin;

当前 Quasar 基于 Vue 3,并且 Plugin 已弃用,我知道在 Vue 3 中应该包含该插件在 /src/main.js 文件中,如下所示:

import { createApp } from 'vue'
const app = createApp({})
app.use(myPlugin, {
  /* optional options */
})

但是在 Quasar 中没有 main.js 文件,我在任何地方都看不到 app.use,并且不知道如何告诉 Quasar使用插件与 axios 拦截器。我知道有一个 quasar.conf.js 文件,您可以设置它来使用 Quasar 插件,例如 Dialog,但我不知道这个文件是否会让 Quasar 使用我编码的插件的地方或者应该如何完成。我无法找到有关此主题的信息,有人可以告诉我如何让 Quasar 插入我的插件或指出我可以在哪里找到有关它的信息吗?谢谢。

I am currently working on a Quasar app that uses axios library. User is authenticated as usual by a token that, when expired, has to be renewed using a renewal token. I need an axios interceptor to filter status 401 so that I can ask for a new token and send the request again.
The interceptor is already coded, now the problem is that I don't know how to make Quasar use the plugin. The app that is currently in production, which is done with Vue 2, does it like this:

const _axios = axios.create(*config*);
axios.interceptors.request.use(.....);

Plugin.install = function(Vue, options) { .... }
Vue.use(Plugin)
export default Plugin;

Currently Quasar is based on Vue 3, and Plugin is deprecated, I know in Vue 3 the plugin should be included in the /src/main.js file like this:

import { createApp } from 'vue'
const app = createApp({})
app.use(myPlugin, {
  /* optional options */
})

But in Quasar there is no main.js file, I can't see an app.use anywhere, and don't know how to tell Quasar to use the plugin with the axios interceptor. I know there's a quasar.conf.js file that you can set to use Quasar plugins, such as Dialog for example,but I don't know if this file would be the place to make Quasar use the plugin I coded or how it should be done. I have not been able to find info on this subject, could someone please tell me how to make Quasar insert my plugin or point me to where I could find info about it? Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文