Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit abe223d

Browse files
committed
feat: add a clearRoutes method
1 parent f7f78f0 commit abe223d

File tree

3 files changed

+35
-3
lines changed

3 files changed

+35
-3
lines changed

‎packages/router/__tests__/matcher/addingRemoving.spec.ts‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,21 @@ describe('Matcher: adding and removing records', () => {
1616
})
1717
})
1818

19+
it('can remove all records', () => {
20+
const matcher = createRouterMatcher([], {})
21+
matcher.addRoute({ path: '/', component })
22+
matcher.addRoute({ path: '/about', component, name: 'about' })
23+
matcher.addRoute({
24+
path: '/with-children',
25+
component,
26+
children: [{ path: 'child', component }],
27+
})
28+
expect(matcher.getRoutes()).not.toHaveLength(0)
29+
matcher.clearRoutes()
30+
expect(matcher.getRoutes()).toHaveLength(0)
31+
expect(matcher.getRecordMatcher('about')).toBeFalsy()
32+
})
33+
1934
it('throws when adding *', () => {
2035
const matcher = createRouterMatcher([], {})
2136
expect(() => {

‎packages/router/src/matcher/index.ts‎

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,9 @@ import type { RouteRecordNameGeneric, _RouteRecordProps } from '../typed-routes'
2727
*/
2828
export interface RouterMatcher {
2929
addRoute: (record: RouteRecordRaw, parent?: RouteRecordMatcher) => () => void
30-
3130
removeRoute(matcher: RouteRecordMatcher): void
3231
removeRoute(name: NonNullable<RouteRecordNameGeneric>): void
33-
32+
clearRoutes: () => void
3433
getRoutes: () => RouteRecordMatcher[]
3534
getRecordMatcher: (
3635
name: NonNullable<RouteRecordNameGeneric>
@@ -345,7 +344,19 @@ export function createRouterMatcher(
345344
// add initial routes
346345
routes.forEach(route => addRoute(route))
347346

348-
return { addRoute, resolve, removeRoute, getRoutes, getRecordMatcher }
347+
function clearRoutes() {
348+
matchers.length = 0
349+
matcherMap.clear()
350+
}
351+
352+
return {
353+
addRoute,
354+
resolve,
355+
removeRoute,
356+
clearRoutes,
357+
getRoutes,
358+
getRecordMatcher,
359+
}
349360
}
350361

351362
function paramsFromLocation(

‎packages/router/src/router.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,11 @@ export interface Router {
239239
*/
240240
getRoutes(): RouteRecord[]
241241

242+
/**
243+
* Delete all routes from the router matcher.
244+
*/
245+
clearRoutes(): void
246+
242247
/**
243248
* Returns the {@link RouteLocation | normalized version} of a
244249
* {@link RouteLocationRaw | route location}. Also includes an `href` property
@@ -1228,6 +1233,7 @@ export function createRouter(options: RouterOptions): Router {
12281233

12291234
addRoute,
12301235
removeRoute,
1236+
clearRoutes: matcher.clearRoutes,
12311237
hasRoute,
12321238
getRoutes,
12331239
resolve,

0 commit comments

Comments
 (0)