question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cannot access $bvToast in Nuxt plugin

See original GitHub issue

Describe the bug

I want to display API errors as toast messages. I have to use a Nuxt plugin to work with Axios error interceptors. The plugin has access to the Vue instance app from the Nuxt context. However, the app instance is missing the $bvToast injection. So calls like app.$bvToast.toast('some toast message') fail.

Steps to reproduce the bug

  1. Create a Nuxt plugin called /plugins/axios-toast.js
export default function ({ $axios, app }) {
  $axios.onError(error => {
    app.$bvToast.toast(error)
  })
}
  1. Reference the plugin in nuxt.config.js
  plugins: [
    '~plugins/axios-toast.js'
  ],
  1. Intentionally trigger an invalid route
<b-button @click="$axios.$get('badroute')">Do it</b-button>
  1. Observe the error in the browser

TypeError: Cannot read property ‘toast’ of undefined

Expected behavior

The Vue instance app in the Nuxt plugin context should have the $bvToast injected.

Versions

Libraries:

  • BootstrapVue: 2.4.1
  • Bootstrap: 4.4.1
  • Nuxt 2.11.0
  • Nuxt Axios 5.9.5

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:16 (9 by maintainers)

github_iconTop GitHub Comments

11reactions
ashwinkshenoycommented, Mar 5, 2020

I used this: $nuxt.$bvToast.toast(error) in my Axios interceptor and works fine!

7reactions
ravgeetdhilloncommented, Jan 16, 2021

In case someone is using Client-Side Rendering only, this issue can be solved by using window.$nuxt

For e.g.

export default function ({ $axios }) {
  $axios.onError(error => {
    window.$nuxt.$bvToast.toast(error)
  })
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to access $bvToast in vue class component
$bvToast is not available directly. There is a workaround however to access the main Vue instance within VueX. Use this._vm.$bvToast instead.
Read more >
Toast | Components - BootstrapVue
A new $bvToast injection (mixin) is created for each Vue virtual machine instance (i.e. each instantiated component), and is not usable via direct...
Read more >
How to run Bootstrapvue Toasts inside vuex Module - Laracasts
I try following code but it doesnt't work.. when I print (this.$bvToast) to console, it gives undefined.. createJob ({commit},
Read more >
[Solved]-How to access $bvToast in vue class component-Vue.js
$bvToast is not available directly. There is a workaround however to access the main Vue instance within VueX. Use this._vm.$bvToast instead. See:.
Read more >
Vue 2.7 and the transition to Composable API - Reddit
Another problem is that this $bvToast is object, is added to components via ... and I can't get access to $bvToast because it...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found