我有一个混合/延迟加载的 Angular/AngularJS 应用程序,它使用新的 Angular 路由器和 ui-router 版本 0.4.2 以及并行出口/视图。为了做到这一点,我遵循了Victor Savkin 的 Lazy Loaded AngularJS 指南。总的来说,这运行良好,我可以在 Angular 路由和 AngularJS 路由之间切换。但是,我遇到了第一次打开页面时未显示 ui-router 子视图的问题。
如果尝试在启动时加载子状态,此问题只会影响状态。在路线之间导航不会遇到同样的问题。我还确定它仅在父状态模板被降级的 Ng2 组件包装时出现在路由中。
状态和模板定义
@Component({ template: '<ng-content></ng-content>'})
export class TestDowngradedComponent { }
angular.module('routerPatchModule', ['ui.router'])
.directive('downgradedComp', downgradeComponent({ component: TestDowngradedComponent }))
.config(['$stateProvider', ($stateProvider) => {
$stateProvider.state({
name: 'parent',
url: '/parent',
template: '<downgraded-comp><ui-view></ui-view></downgraded-comp>',
})
.state({
name: 'parent.test',
url: '/test',
template: 'The child route doesn't appear on load',
})
}]);
/**
* Ng2 Module which upgrades the legacy Ng1 module
*/
@NgModule({
declarations: [
EmptyTemplateComponent,
TestDowngradedComponent,
],
entryComponents: [
TestDowngradedComponent,
],
imports: [
SharedModule,
UpgradeModule,
RouterModule.forChild([
{path: '**', component: EmptyTemplateComponent},
]),
],
})
export class LegacyAppModule {
constructor(upgrade: UpgradeModule) {
upgrade.bootstrap(document.body, [ng1AppModule, 'routerPatchModule'], { strictDi: true });
setUpLocationSync(upgrade);
}
}
跟踪状态更改事件,我能够确定子视图被临时加载但随后被删除。
状态事件日志
$viewContent Loading undefined {"view":"@"}
$viewContent Loaded undefined {"view":"@"}
$stateChange Start state1.child
$stateChange Success state1.child
$viewContent Loading undefined {"view":"@"}
$viewContent Loading state1 {"view":"@state1"}
$viewContent Loaded state1.child {"view":"@state1"} // child view temporarily loaded
$viewContent Loaded state1 {"view":"@"} // child view gone!
// User clicks link to state2
$stateChange Start state2
$stateChange Success state2
$viewContent Loading undefined {"view":"@"}
$viewContent Loading state2 {"view":"@state2"}
$viewContent Loaded state2 {"view":"@state2"}
$viewContent Loaded state2 {"view":"@"}
$stateChange Start state2.child
$stateChange Success state2.child
$viewContent Loading state2 {"view":"@state2"}
$viewContent Loaded state2.child {"view":"@state2"} // Child loaded successfully
// User clicks link to state1
$stateChange Start state1
$stateChange Success state1
$viewContent Loading undefined {"view":"@"}
$viewContent Loading state1 {"view":"@state1"}
$viewContent Loaded state1 {"view":"@state1"}
$viewContent Loaded state1 {"view":"@"}
$stateChange Start state1.child
$stateChange Success state1.child
$viewContent Loading state1 {"view":"@state1"}
$viewContent Loaded state1.child {"view":"@state1"} // Child loaded after parent
使用<ng-content>
导致 ui-router 无法正确呈现的组件怎么样?我可以做些什么来纠正这个问题?