简体   繁体   English

Angular2动态组件

[英]Angular2 Dynamic Components

I'm pretty new to Angular2 and I haven't found a solution to what I'm trying to do. 我对Angular2很新,我还没有找到解决方案,我正在尝试做什么。 I'll try to explain with the following example. 我将尝试用以下示例进行解释。

Let's say we have a parent component A which could include either a child component X or Y . 假设我们有一个父组件A ,它可以包含子组件XY. That could be easily solved using a viewchild. 使用viewchild可以很容易地解决这个问题。

But, what if we wanted to load the child components dynamically without code duplication? 但是, 如果我们想在没有代码重复的情况下动态加载子组件呢?

Real example : 真实的例子:

  • Component U : Component called by the user which calls the component A and injects a child component (X or Y) according to the user's choice. 组件U:用户调用的组件,它调用组件A并根据用户的选择注入子组件(X或Y)。
  • Component A : Grid container which could be a table, a panel or whatever. 组件A:网格容器,可以是表,面板或其他任何东西。
  • Component X : View containing rows for the service S1 组件X:包含服务S1的行的视图
  • Component Y : View containing rows for the service S2 组件Y:包含服务S2的行的视图

The aim would be to easily change the container, changing all the views automatically . 目的是轻松更改容器,自动更改所有视图。

There must be a way to do that, I just can't find a good and clean solution for that sort of need. 必须有办法做到这一点,我找不到满足这种需求的良好和干净的解决方案。 Any help would be much appreciated. 任何帮助将非常感激。

Thanks 谢谢

You can use the Angular2 Component Router as so: 您可以使用Angular2组件路由器

(Note: For simplicity reasons, I have not added input elements and instead, used a elements. The same procedure applies though, call a function to tell the router to insert the loaded component into the <router-outlet> ) (注意:为了简单起见,我没有添加输入元素,而是使用a元素。虽然调用一个函数来告诉路由器将加载的组件插入<router-outlet> ,但是同样的过程适用)

app.component.ts app.component.ts

import { Component }       from '@angular/core';
import { Router }       from '@angular/router';

@Component({
    selector: 'my-app',
    template: `
        <div>
            <!-- to show how the function can be called -->
            <a href="#" (click)="load('/page1')">Page 1</a>

            <!-- Right way with links -->
            <a href="#" routerLink="/page1">Page 1</a>

            <router-outlet></router-outlet>
        </div>
    `
})
export class AppComponent {
    constructor(private router: Router)

    load(path: string): void {
        this.router.navigate([path]);
    }
}

app.routing.ts app.routing.ts

import { ModuleWithProviders }   from '@angular/core';
import { Routes, RouterModule }  from '@angular/router';

const appRoutes: Routes = [
    {
        path: "page1",
        component: "PageOneComponent"
    }
];

export const appRoutingProviders: any[] = [

];

export const appRouting: ModuleWithProviders = RouterModule.forRoot(appRoutes);

app.module.ts app.module.ts

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';

import { AppComponent } from './app.component';

import { appRouting, appRoutingProviders }  from './app.routing';

@NgModule({
    imports: [
        BrowserModule,
        FormsModule,
        appRouting,
    ],
    declarations: [
        RootComponent,
    ],
    providers: [
        appRoutingProviders
    ],
    bootstrap: [AppComponent]
})
export class AppModule { }

Cheers! 干杯!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM