File tree Expand file tree Collapse file tree 2 files changed +38
-2
lines changed Expand file tree Collapse file tree 2 files changed +38
-2
lines changed Original file line number Diff line number Diff line change @@ -64,10 +64,22 @@ p.abort = function () {
64
64
65
65
/**
66
66
* Abort current transition and redirect to a new location.
67
+ *
68
+ * @param {String } path
67
69
*/
68
70
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
+ }
71
83
}
72
84
73
85
/**
Original file line number Diff line number Diff line change @@ -394,6 +394,21 @@ describe('Core', function () {
394
394
template : '<div><router-view></router-view></div>'
395
395
} )
396
396
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
+ } ,
397
412
'*' : {
398
413
component : {
399
414
template : '<p>default</p>'
@@ -408,6 +423,11 @@ describe('Core', function () {
408
423
transition . abort ( )
409
424
next ( )
410
425
} , 100 )
426
+ } else if ( transition . to . path . indexOf ( '/redirect' ) > - 1 ) {
427
+ setTimeout ( function ( ) {
428
+ transition . redirect ( '/to/:id' )
429
+ next2 ( )
430
+ } , 100 )
411
431
} else {
412
432
transition . next ( )
413
433
}
@@ -418,6 +438,10 @@ describe('Core', function () {
418
438
router . go ( '/no' )
419
439
function next ( ) {
420
440
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' )
421
445
done ( )
422
446
}
423
447
} )
You can’t perform that action at this time.
0 commit comments