Skip to content

Commit 400b1c6

Browse files
committed
implement transition.redirect
1 parent c391b85 commit 400b1c6

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

src/transition.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,22 @@ p.abort = function () {
6464

6565
/**
6666
* Abort current transition and redirect to a new location.
67+
*
68+
* @param {String} path
6769
*/
6870

69-
p.redirect = function () {
70-
// TODO
71+
p.redirect = function (path) {
72+
/* istanbul ignore else */
73+
if (!this.aborted) {
74+
this.aborted = true
75+
this.to._aborted = true
76+
path = util.mapParams(path, this.to.params, this.to.query)
77+
this.router.replace(path)
78+
} else {
79+
util.warn(
80+
'Don\'t call redirect() on an already aborted transition.'
81+
)
82+
}
7183
}
7284

7385
/**

test/unit/specs/core.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,21 @@ describe('Core', function () {
394394
template: '<div><router-view></router-view></div>'
395395
})
396396
router.map({
397+
'/no': {
398+
component: {
399+
template: '<p>NO</p>'
400+
}
401+
},
402+
'redirect/:id': {
403+
component: {
404+
template: 'should never show'
405+
}
406+
},
407+
'/to/:id': {
408+
component: {
409+
template: 'to {{$route.params.id}}'
410+
}
411+
},
397412
'*': {
398413
component: {
399414
template: '<p>default</p>'
@@ -408,6 +423,11 @@ describe('Core', function () {
408423
transition.abort()
409424
next()
410425
}, 100)
426+
} else if (transition.to.path.indexOf('/redirect') > -1) {
427+
setTimeout(function () {
428+
transition.redirect('/to/:id')
429+
next2()
430+
}, 100)
411431
} else {
412432
transition.next()
413433
}
@@ -418,6 +438,10 @@ describe('Core', function () {
418438
router.go('/no')
419439
function next () {
420440
expect(router.app.$el.textContent).toBe('default')
441+
router.go('/redirect/12345')
442+
}
443+
function next2 () {
444+
expect(router.app.$el.textContent).toBe('to 12345')
421445
done()
422446
}
423447
})

0 commit comments

Comments
 (0)