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.

How to call BToast from outside Vue

See original GitHub issue

Hi, I want to use the BToast component outside of Vue.js, is there any way I can call it? For example, I want to launch a toast whenever a request I make with axios results in an error. So I define my interceptors:

const service = axios.create({
  baseURL: '/api',
  timeout: 10000,
});

service.interceptors.response.use(
  response => {
    return response.data;
  },
  error => {
    // How can I call a Toast from here? <----
    return Promise.reject(error);
  }
);

Thank you very much in advance

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:3
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

14reactions
gmw-webcommented, May 13, 2020

I have a somewhat similar setup and do the following:

In your main file, export your app instance:

import Vue from "vue";
import { BootstrapVue } from "bootstrap-vue";

[...]
Vue.use(BootstrapVue);

export const app = new Vue({
  store,
  router,
  i18n,
  render: h => h(App)
}).$mount("#app");

And in my Axios setup file, I have the following:

import axios from "axios";
import { app } from "@/main";

const config: AxiosRequestConfig = {
  baseURL: "/api/v1/"
};

const api = axios.create(config);

// response interceptor
api.interceptors.response.use(
  function(response) {
    // Successful response, return data
    return response;
  },
  function(error) {
    app.$bvToast.toast(`An error occurred. API unreachable.`, {
      title: `Server unreachable`,
      variant: "danger"
    });

    return Promise.reject(error);
  }
);

[...]
2reactions
leo95workscommented, May 5, 2020

Hello @tmorehouse , thank you very much for your response, but your solution returns the following message:

[BootstrapVue warn]: BToast - "$bvToast" must be accessed from a Vue instance "this" context.

I show you the complete file with which I am catching axios errors.

import Vue from 'vue';
import axios from 'axios';

const vm = new Vue({});

const service = axios.create({
  baseURL: /api,
  timeout: 10000,
});

service.interceptors.request.use(
  config => {
    return config;
  },
  error => {
    return Promise.reject(error);
  }
);

service.interceptors.response.use(
  response => {
    return response.data;
  },
  error => {
    vm.$bvToast.toast('Something went wrong');

    return Promise.reject(error);
  }
);

export default service;
Read more comments on GitHub >

github_iconTop Results From Across the Web

How can I put toast messages in a separate file when unsing ...
Create an external file and import that file into your main.js. toasts.js import Vue from 'vue' import Toasted from 'vue-toasted' ...
Read more >
Toast | Components - BootstrapVue
bvToast.toast() are children of the Vue instance that calls the this.$bvToast.toast() method, and will be hidden and destroyed if that Vue instance (i.e. ......
Read more >
How to run Bootstrapvue Toasts inside vuex Module - Laracasts
This is assuming you've installed bootstrap vue through npm or yarn. import { ToastPlugin } from 'bootstrap-vue' Vue. use(ToastPlugin) const app = new...
Read more >
vue-on-toast - npm
Start using vue-on-toast in your project by running `npm i ... must be included in your application, preferably outside of your router.
Read more >
Show and Hide the Toast - DevExtreme - DevExpress
With Angular, Vue, or React, use a different technique. Bind the visible property of the Toast UI component to a component property.
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