Skip to content

Commit e087ea4

Browse files
committed
add redirect for notfound route
1 parent 674e35e commit e087ea4

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

src/index.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ class Router {
6161
this._currentTransition = null
6262
this._previousTransition = null
6363
this._notFoundHandler = null
64+
this._notFoundRedirect = null
6465
this._beforeEachHooks = []
6566
this._afterEachHooks = []
6667

@@ -315,7 +316,11 @@ class Router {
315316
*/
316317

317318
_addRedirect (path, redirectPath) {
318-
this._addGuard(path, redirectPath, this.replace)
319+
if (path === '*') {
320+
this._notFoundRedirect = redirectPath
321+
} else {
322+
this._addGuard(path, redirectPath, this.replace)
323+
}
319324
}
320325

321326
/**
@@ -363,6 +368,12 @@ class Router {
363368
if (matched) {
364369
matched[0].handler(matched[0], matched.queryParams)
365370
return true
371+
} else if (this._notFoundRedirect) {
372+
matched = this._recognizer.recognize(path)
373+
if (!matched) {
374+
this.replace(this._notFoundRedirect)
375+
return true
376+
}
366377
}
367378
}
368379

test/unit/specs/core.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -529,15 +529,17 @@ describe('Core', function () {
529529
})
530530
router.redirect({
531531
'/whatever': '/a/b',
532-
'/ok': '/a/c'
532+
'/ok': '/a/c',
533+
'*': '/a/b'
533534
})
534535
var App = Vue.extend({
535536
template: '<div><router-view></router-view></div>'
536537
})
537538
router.start(App, el)
538539
assertRoutes([
539540
['/whatever', 'hello'],
540-
['/ok?msg=world', 'world']
541+
['/ok?msg=world', 'world'],
542+
['/fesfsefsef', 'hello']
541543
], done)
542544
})
543545

0 commit comments

Comments
 (0)