Skip to content

Commit b17be85

Browse files
committed
refactor(guards): warn once
1 parent 9520d66 commit b17be85

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

__tests__/warnings.spec.ts

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -194,9 +194,31 @@ describe('warnings', () => {
194194
],
195195
})
196196
await router.push('/foo')
197-
expect(
198-
`Component "default" in record with path "/foo" is defined using "defineAsyncComponent()". Write "() => import('./MyPage.vue')" instead of "defineAsyncComponent(() => import('./MyPage.vue'))"`
199-
).toHaveBeenWarned()
197+
expect(`defined using "defineAsyncComponent()"`).toHaveBeenWarned()
198+
})
199+
200+
it('warns if use defineAsyncComponent in routes only once per component', async () => {
201+
const router = createRouter({
202+
history: createMemoryHistory(),
203+
// simulates import('./component.vue')
204+
routes: [
205+
{ path: '/', component },
206+
{
207+
path: '/foo',
208+
component: defineAsyncComponent(() => Promise.resolve({})),
209+
},
210+
{
211+
path: '/bar',
212+
component: defineAsyncComponent(() => Promise.resolve({})),
213+
},
214+
],
215+
})
216+
await router.push('/foo')
217+
await router.push('/')
218+
await router.push('/foo')
219+
expect(`defined using "defineAsyncComponent()"`).toHaveBeenWarnedTimes(1)
220+
await router.push('/bar')
221+
expect(`defined using "defineAsyncComponent()"`).toHaveBeenWarnedTimes(2)
200222
})
201223

202224
it('warns if no route matched', async () => {

src/navigationGuards.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,8 +263,10 @@ export function extractComponentsGuards(
263263
rawComponent = () => promise
264264
} else if (
265265
(rawComponent as any).__asyncLoader &&
266-
guardType === 'beforeRouteEnter'
266+
// warn only once per component
267+
!(rawComponent as any).__warnedDefineAsync
267268
) {
269+
;(rawComponent as any).__warnedDefineAsync = true
268270
warn(
269271
`Component "${name}" in record with path "${record.path}" is defined ` +
270272
`using "defineAsyncComponent()". ` +

0 commit comments

Comments
 (0)