Skip to content

Commit 00b8dff

Browse files
committed
fix async component
1 parent 400b1c6 commit 00b8dff

File tree

4 files changed

+73
-16
lines changed

4 files changed

+73
-16
lines changed

src/pipeline.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ exports.canDeactivate = function (view, transition, next) {
5858
exports.canActivate = function (handler, transition, next) {
5959
util.resolveAsyncComponent(handler, function (Component) {
6060
// have to check due to async-ness
61-
if (transition.to._aborted) {
61+
if (transition.aborted) {
6262
return
6363
}
6464
// determine if this component can be activated

src/router/internal.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,12 @@ module.exports = function (Vue, Router) {
137137
return
138138
}
139139

140-
// construct route context
140+
// abort previous transition
141+
if (this._currentTransition) {
142+
this._currentTransition.aborted = true
143+
}
144+
145+
// construct new route and transition context
141146
var route = new Route(path, this)
142147
var transition = this._currentTransition =
143148
new RouteTransition(this, route, previousRoute)
@@ -152,6 +157,7 @@ module.exports = function (Vue, Router) {
152157
})
153158
}
154159

160+
// check global before hook
155161
var before = this._beforeEachHook
156162
var startTransition = function () {
157163
transition.start(function () {
@@ -231,7 +237,7 @@ module.exports = function (Vue, Router) {
231237
comp = handler.component = Vue.extend(comp)
232238
}
233239
/* istanbul ignore if */
234-
if (typeof comp !== 'function' || !comp.cid) {
240+
if (typeof comp !== 'function') {
235241
handler.component = null
236242
routerUtil.warn(
237243
'invalid component for route "' + handler.path + '"'

src/transition.js

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,6 @@ var pipeline = require('./pipeline')
1212
*/
1313

1414
function Transition (router, to, from) {
15-
// mark previous route as aborted
16-
if (from) {
17-
from._aborted = true
18-
}
19-
2015
this.router = router
2116
this.to = to
2217
this.from = from
@@ -57,7 +52,6 @@ var p = Transition.prototype
5752
p.abort = function () {
5853
if (!this.aborted) {
5954
this.aborted = true
60-
this.to._aborted = true
6155
this.router.replace(this.from.path || '/')
6256
}
6357
}
@@ -69,16 +63,10 @@ p.abort = function () {
6963
*/
7064

7165
p.redirect = function (path) {
72-
/* istanbul ignore else */
7366
if (!this.aborted) {
7467
this.aborted = true
75-
this.to._aborted = true
7668
path = util.mapParams(path, this.to.params, this.to.query)
7769
this.router.replace(path)
78-
} else {
79-
util.warn(
80-
'Don\'t call redirect() on an already aborted transition.'
81-
)
8270
}
8371
}
8472

@@ -203,7 +191,7 @@ p.callHook = function (hook, context, cb, expectBoolean, cleanup) {
203191
return
204192
}
205193
nextCalled = true
206-
if (!cb || transition.to._aborted) {
194+
if (!cb || transition.aborted) {
207195
return
208196
}
209197
cb(data)

test/unit/specs/core.js

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,69 @@ describe('Core', function () {
534534
history.back()
535535
})
536536
})
537+
538+
it('async component', function (done) {
539+
router = new Router({
540+
abstract: true
541+
})
542+
router.map({
543+
'/a': {
544+
component: function (resolve) {
545+
setTimeout(function () {
546+
resolve({
547+
template: 'hello!'
548+
})
549+
}, wait)
550+
}
551+
}
552+
})
553+
router.start(Vue.extend({
554+
template: '<div><router-view></router-view></div>'
555+
}), el)
556+
router.go('/a')
557+
expect(router.app.$el.textContent).toBe('')
558+
setTimeout(function () {
559+
expect(router.app.$el.textContent).toBe('hello!')
560+
done()
561+
}, wait * 2)
562+
})
563+
564+
it('async component abort', function (done) {
565+
var spy = jasmine.createSpy('async-component-abort')
566+
router = new Router({
567+
abstract: true
568+
})
569+
router.map({
570+
'/a': {
571+
component: function (resolve) {
572+
setTimeout(function () {
573+
resolve({
574+
template: 'hello!',
575+
created: spy
576+
})
577+
}, wait)
578+
}
579+
},
580+
'/b': {
581+
component: {
582+
template: 'B'
583+
}
584+
}
585+
})
586+
router.start(Vue.extend({
587+
template: '<div><router-view></router-view></div>'
588+
}), el)
589+
router.go('/a')
590+
expect(router.app.$el.textContent).toBe('')
591+
setTimeout(function () {
592+
router.go('/b')
593+
setTimeout(function () {
594+
expect(router.app.$el.textContent).toBe('B')
595+
expect(spy).not.toHaveBeenCalled()
596+
done()
597+
}, wait * 2)
598+
}, 0)
599+
})
537600
}
538601

539602
function assertRoutes (matches, options, done) {

0 commit comments

Comments
 (0)