了解NUXT中的上下文和应用程序方法

时间:2020-01-21 08:15:33

标签: vue.js nuxt.js bugsnag

我正在尝试在python manage.py runserver中使用bugsnagClient及其notify方法,在plugins/axios.js中有此代码

plugins/bugsnag.js

我想将方法​​附加到import Vue from "vue" import bugsnag from "@bugsnag/js" import bugsnagVue from "@bugsnag/plugin-vue" // const bugsnagClient = bugsnag(`${process.env.BUGSNAG_API_KEY}`) var bugsnagClient = bugsnag({ apiKey: "", notifyReleaseStages: ["production"] }) bugsnagClient.use(bugsnagVue, Vue) app

context

我想在export default ({ app }, inject) => { function bugsnagNotify(error) { return bugsnagClient.notify(new Error(error)) } // Set the function directly on the context.app object app.bugsnagNotify = bugsnagNotify }

中使用它
plugins/axios.js

在此文件中,当我只为export default function({ store, app }) { if (store.getters.token) { console.log(app.bugsnagNotify("ss")) app.$axios.setToken(store.getters.token, "Bearer") } else { //app.$bugsnag.notify(new Error("Bearer tooken is missing in Axios request.")) } } 做console.log

我可以看到app

但是当我打电话给bugsnagNotify: ƒ bugsnagNotify(error)时,只会收到诸如app.bugsnagNotify("error")这样的错误消息

我也在VM73165:37 TypeError: app.bugsnagNotify is not a function

中尝试过
plugins/bugsnag.js

我只得到一个错误

export default (ctx, inject) => {
  inject('bugsnag', bugsnagClient)
}

1 个答案:

答案 0 :(得分:0)

如果要在一个插件内部注入上下文,并想在另一个插件内部使用该功能,则需要确保要注入的插件首先位于nuxt.config.js

...
plugins: [
  '~/plugins/bugsnag.js',
  '~/plugins/axios.js'
],
...