我的angular2应用程序的根组件有以下模板:
<main class="main">
<div class="main__container">
<div class="main__left-area">
<router-outlet name="left-zone"></router-outlet>
</div>
<div class="main__right-area">
<router-outlet name="right-zone"></router-outlet>
</div>
</div>
</main>
以下路线:
import { HomeSummaryComponent } from "../components/home-summary/home-summary.component"
import { DataSummaryComponent } from "../components/data-summary/data-summary.component"
import { AskSummaryComponent } from "../components/ask-summary/ask-summary.component"
import { Routes } from "@angular/router"
export const AppRoutes: Routes = [
{
path: "",
component: HomeSummaryComponent,
outlet: "left-zone"
},
{
path: "",
component: DataSummaryComponent,
outlet: "right-zone"
}
];
它实际上效果很好,但我希望能够在&#34;右区&#34;中加载多个组件。 (实际上在左边一个)。想法是这样的:
{
path: "",
components: [ DataSummaryComponent, AskSummaryComponent ],
outlet: "right-zone"
}
控制器将一个接一个地附加。目前是否支持?如果不是,是否可以扩展现有的RouterOutlet呢?
由于
答案 0 :(得分:7)
只能为路线分配一个组件,您可以做的是添加由DataSummaryComponent & AskSummaryComponent
组成的父组件,并在路径中对其进行配置。
更新
如评论中所述,您可以创建一个通用组件,使用路径中的数据属性配置动态加载组件,
路线配置
const appRoutes: Routes = [
{ path: '', redirectTo: '/route1', pathMatch: 'full' },
{ path: 'route1', component: MasterComponent, data: {puppets : [PuppetComponent1]} },
{ path: 'route2', component: MasterComponent, data: {puppets : [PuppetComponent2, PuppetComponent3]}},
{ path: 'route3', component: MasterComponent, data: {puppets : [PuppetComponent1, PuppetComponent2, PuppetComponent4]}}
];
主要组件
@Component({
template: `<h1>I am the Master of components</h1>
<hr />
<div #puppetContainer></div>
`
})
class MasterComponent {
@ViewChild('puppetContainer', { read: ViewContainerRef }) puppetContainer: ViewContainerRef;
constructor(
private router: Router,
private route: ActivatedRoute,
private _componentFactoryResolver: ComponentFactoryResolver,
private viewContainer: ViewContainerRef) {
}
ngOnInit(){
this.route.data
.subscribe(data => {
if(!!data && !!data.puppets && data.puppets.length > 0){
data.puppets.map(puppet => {
let componentFactory = this._componentFactoryResolver.resolveComponentFactory(puppet);
this.puppetContainer.createComponent(componentFactory);
});
}
});
}
}
选中此Plunker!!
希望这会有所帮助!!