使用angular2在同一路由器出口中加载多个组件

苏涅兹

我的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"
  }
];

它实际上工作得很好,但是我希望能够在“右侧区域”(以及实际上在左侧区域)中加载多个组件。这个想法将是这样的:

{
  path: "",
  components: [ DataSummaryComponent, AskSummaryComponent ],
  outlet: "right-zone"
}

控制器将被一个接一个地附加。目前支持吗?如果否,是否可以扩展现有的RouterOutlet?

谢谢

Madhu ranjan

只能为一个路由分配一个组件,您可以做的是添加一个由该组件组成的父组件,DataSummaryComponent & AskSummaryComponent并在该路由中对其进行配置。

更新:

如评论中所述,您可以创建一个通用组件,该组件使用route中的数据属性配置动态加载组件,

路由配置

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);
                });
              }
           });
    }

}

检查这个柱塞!

希望这可以帮助!!

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

将嵌套的路由加载到同一路由器出口中

同一路由的页面上的多个路由器出口

如何在同一路由器链接上使用多个URL

Angular2路由器:错误:找不到要加载“ InboxComponent”的主要出口

如何在同一路径的模板中提供多个路由器出口?

角2,另一路由器出口内的RC5路由器出口

angular2路由器导航无法正确加载组件

Angular2路由器出口组件事件发出(A2> = v1.2.0)

使用到达路由器拆分同一路由器中的路由

如何知道Angular 5及更高版本的路由器出口中加载了哪个组件

使用Angular 2路由器将不同的值传递给同一组件

使用loadChildren的angular2路由器延迟加载服务不是单例

组件模板中的Angular 2路由器出口

反应路由器给出“重定向到同一路由”错误

获取Angular2路由器中活动路由上的组件实例?

Angular2路由器-辅助路由

快速路由:同一路由的多个 URL

检查两个IP地址是否属于同一路由器

Angular2路由器插座组件的样式正固定

Angular2路由器会附加组件而不是替换它

如何使用路由器出口加载Angular组件

Angular2路由器导航

Angular2路由器和导航

Angular2路由器错误:找不到主插座以加载“主页”

Angular2路由器弃用的依赖项未加载

Angular2路由器:找不到要加载“ HomeComponent”的主要插座

Angular4路由器,延迟加载的模块子路由,命名为路由器出口

如何使用Angular 5将两个路由器插座用于同一路径

Angular2多个路由器出口