Skip to content

Commit d49aaa1

Browse files
committed
refactor: throw error in useVfm
1 parent 47cf520 commit d49aaa1

File tree

3 files changed

+14
-23
lines changed

3 files changed

+14
-23
lines changed

packages/vue-final-modal/src/useApi.ts

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,22 @@ import type CoreModal from './components/CoreModal/CoreModal.vue'
66
import { internalVfmSymbol } from './injectionSymbols'
77

88
import type { ComponentProps, Constructor, InternalVfm, ModalSlot, ModalSlotOptions, RawProps, UseModalOptions, UseModalOptionsPrivate, UseModalReturnType, Vfm } from './Modal'
9-
import { activeVfm, getActiveVfm } from './plugin'
9+
import { getActiveVfm } from './plugin'
1010

1111
/**
1212
* Returns the vfm instance. Equivalent to using `$vfm` inside
1313
* templates.
1414
*/
1515
export function useVfm(): Vfm {
1616
const vfm = getActiveVfm()
17-
if (__DEV__ && !vfm)
18-
consoleError()
17+
if (__DEV__ && !vfm) {
18+
throw new Error(
19+
'[Vue Final Modal]: getActiveVfm was called with no active Vfm. Did you forget to install vfm?\n'
20+
+ '\tconst vfm = createVfm()\n'
21+
+ '\tapp.use(vfm)\n'
22+
+ 'This will fail in production.',
23+
)
24+
}
1925

2026
return vfm!
2127
}
@@ -80,10 +86,6 @@ export function useModal<P = InstanceType<typeof VueFinalModal>['$props']>(_opti
8086
async function open(): Promise<string> {
8187
await nextTick()
8288
const vfm = useVfm()
83-
if (!vfm) {
84-
consoleError()
85-
return Promise.resolve('error')
86-
}
8789
if (options.modelValue)
8890
return Promise.resolve('[Vue Final Modal] modal is already opened.')
8991

@@ -152,10 +154,6 @@ export function useModal<P = InstanceType<typeof VueFinalModal>['$props']>(_opti
152154

153155
function destroy(): void {
154156
const vfm = useVfm()
155-
if (!vfm) {
156-
consoleError()
157-
return
158-
}
159157
const index = vfm.dynamicModals.indexOf(options)
160158
if (index !== -1)
161159
vfm.dynamicModals.splice(index, 1)
@@ -221,14 +219,3 @@ export function useVfmAttrs(options: {
221219

222220
return vfmAttrs
223221
}
224-
225-
function consoleError() {
226-
if (__DEV__ && !activeVfm) {
227-
throw new Error(
228-
'[Vue Final Modal]: getActiveVfm was called with no active Vfm. Did you forget to install vfm?\n'
229-
+ '\tconst vfm = createVfm()\n'
230-
+ '\tapp.use(vfm)\n'
231-
+ 'This will fail in production.',
232-
)
233-
}
234-
}

viteplay/src/components/VueFinalModal/Basic.example.vue

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@ import TestModal from './TestModal.vue'
88
console.log('modal → ', modal)
99
1010
const { toggle, closeAll } = useVfm()
11-
// modal.open()
11+
modal.open().then((res) => { console.log('res', res) })
12+
modal.open().then((res) => { console.log('res', res) })
13+
modal.open().then((res) => { console.log('res', res) })
14+
modal.open().then((res) => { console.log('res', res) })
1215
const modal1 = useModal({
1316
keepAlive: true,
1417
component: VueFinalModal,

viteplay/src/components/VueFinalModal/modalsHelpers.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { VueFinalModal, useModal, useModalSlot } from 'vue-final-modal'
22
import DefaultSlot from '../DefaultSlot.vue'
33

4+
console.log('helper')
45
export const modal = useModal({
56
component: VueFinalModal,
67
// defaultModelValue: true,

0 commit comments

Comments
 (0)