Cannot access $bvToast in Nuxt plugin
See original GitHub issueDescribe 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
- Create a Nuxt plugin called
/plugins/axios-toast.js
export default function ({ $axios, app }) {
$axios.onError(error => {
app.$bvToast.toast(error)
})
}
- Reference the plugin in
nuxt.config.js
plugins: [
'~plugins/axios-toast.js'
],
- Intentionally trigger an invalid route
<b-button @click="$axios.$get('badroute')">Do it</b-button>
- 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:
- Created 4 years ago
- Reactions:1
- Comments:16 (9 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I used this:
$nuxt.$bvToast.toast(error)
in my Axios interceptor and works fine!In case someone is using Client-Side Rendering only, this issue can be solved by using
window.$nuxt
For e.g.