Skip to content

Instantly share code, notes, and snippets.

@tkhduracell
Created September 5, 2025 15:46
Show Gist options
  • Save tkhduracell/b047e3b82d48fffa84011ed4d2c6955b to your computer and use it in GitHub Desktop.
Save tkhduracell/b047e3b82d48fffa84011ed4d2c6955b to your computer and use it in GitHub Desktop.
Access this.$bvmodal from Vue 2.7 setup syntax
/**
* Usage:
* const { msgBoxConfirm } = useModal();
*/
import { getCurrentInstance } from 'vue'
export function useModal () {
const { proxy } = getCurrentInstance() ?? {}
if (!proxy) {
throw new Error('No instance in context')
}
const $bvModal = proxy.$bvModal
if (!$bvModal) {
throw new Error('No $bvModal in context')
}
const createElement = getCurrentInstance()?.proxy.$createElement
if (!createElement) {
throw new Error('No $createElement in context')
}
const {
hide,
msgBoxConfirm,
msgBoxOk,
show
} = $bvModal
if (!hide || !msgBoxConfirm || !msgBoxOk || !show) {
throw new Error('Incomplete $bvModal in context')
}
return {
hide: hide.bind($bvModal),
msgBoxConfirm: msgBoxConfirm.bind($bvModal),
msgBoxOk: msgBoxOk.bind($bvModal),
show: show.bind($bvModal),
createElement: createElement.bind(proxy)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment