Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to call SnackbarProgrammatic outside of Vue instance #188

Open
mateuswetah opened this issue Jan 30, 2024 · 6 comments
Open

Unable to call SnackbarProgrammatic outside of Vue instance #188

mateuswetah opened this issue Jan 30, 2024 · 6 comments

Comments

@mateuswetah
Copy link

Overview of the problem

Buefy-next version: 0.1.2
Vuejs version: 3.4.15
OS/Browser: Firefox, Linux Ubuntu

Description

Previously, I could instantiate Snackbar or even Modal by using something like

import { SnackbarProgrammatic as Snackbar } from 'buefy';

Snackbar.open(...);

This was useful in situations like this, where I'm passing the error handler to use Snackbar as an alert every time axios detects an error.

In the new version, I'm getting an error, and it is a weird one... it does not outputs clearly the issue, but debugging a little bit, it seems that Snackbar.open yields undefined 😢.

I'm I doing something wrong? Would it be possible some other way, without access to $buefy?

@kikuomax
Copy link
Collaborator

@mateuswetah If it is in a setup script, there is a workaround:

Since there is no global Vue instance in Vue 3, you need somehow specify a Vue app instance. If you have a Vue app instance known, you can do something like below:

import { ModalProgrammatic } from '@ntohq/buefy-next'

// somehow you know a Vue app instance
const modalProgrammatic = new ModalProgrammatic(app)
modalProgrammatic.open({
  // parameters ...
})

The above behavior was newly introduced in buefy-next, though, it is subject to change.

@mateuswetah
Copy link
Author

Ok, I'll see if I can figure a way to pass the app instance there... but a question. If I do manage to do it, wouldn't it be the same thing of doing something like this?

app.config.globalProperties.$buefy.snackbar.open(...

@mateuswetah
Copy link
Author

Oh and now that I read the discussion, I do like the useModal strategy, let us know when it is released in the npm package :)

@mateuswetah
Copy link
Author

BUT... giving a try,

import { getCurrentInstance } from 'vue';

const self = getCurrentInstance(); 

At least directly in my axios.js file returns null for the current instance... maybe this won't happen in the useModal scenario because it would come from the Buefy context?

@kikuomax
Copy link
Collaborator

BUT... giving a try,

import { getCurrentInstance } from 'vue';

const self = getCurrentInstance(); 

At least directly in my axios.js file returns null for the current instance... maybe this won't happen in the useModal scenario because it would come from the Buefy context?

Since getCurrentInstance is an internal API of Vue, there is no official documentation about it. But I am sure getCurrentInstance returns non-null if it is called directly inside <script setup>. I have not tested myself but it might return non-null in lifecycle hooks, e.g., onMounted, and watch callbacks.

@mateuswetah
Copy link
Author

mateuswetah commented Jan 31, 2024

Oh ok, didn't payed attention to the <script setup> context. Thing is, my axios.js file is imported not only in components, but in Vuex modules, for example, some stuff completely out of the Vue area. I think I'll have to refactor this logic somehow.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants