Created
September 5, 2025 15:46
-
-
Save tkhduracell/b047e3b82d48fffa84011ed4d2c6955b to your computer and use it in GitHub Desktop.
Access this.$bvmodal from Vue 2.7 setup syntax
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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