【问题标题】:Angular 2 insert a @Component into the DOM of another componentAngular 2 将 @Component 插入到另一个组件的 DOM 中
【发布时间】:2017-03-26 20:20:12
【问题描述】:

我有一个组件

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

@Component({
  selector: 'test-component',
  template: '<b>Content</b>',
})
export class TestPage {
  constructor() {}
}

我还有另一个组件:

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

@Component({
  selector: 'main-component',
  templateUrl: 'main.html',
})
export class MainPage {

  constructor() {}

  putInMyHtml() {

  }
}

main.html:

<p>stuff</p>
<div> <!-- INSERT HERE --> </div>

我如何动态地将我的TestPage 组件插入到&lt;!--INSERT HERE--&gt; 以编程方式运行的区域中,就像我运行putInMyHtml 时一样。

我尝试编辑 DOM 并插入 &lt;test-component&gt;&lt;/test-component&gt;,但它不显示来自 TestPage 模板的内容文本。

【问题讨论】:

标签: javascript angular


【解决方案1】:

如果两个组件都在同一个模块中,请确保它们都首先声明:

我的模块

@NgModule({
  declarations: [TestPage, MainComponent]
})

如果它们位于不同的模块中,请确保您已导出 TestPage 并将其导入到您加载 MainComponent 的模块中:

TestPageModule

@NgModule({
  declarations: [TestPage],
  exports: [TestPage]
})

主组件

@NgModule({
  declarations: [MainComponent],
  imports: [TestPage]
})

【讨论】:

    【解决方案2】:

    这是一个带有 ComponentFactoryResolverPlunker Example

    首先你必须正确注册你的动态组件TestPage

    app.module.ts

    @NgModule({
      declarations: [MainPage, TestPage],
      entryComponents: [TestPage]
    })
    

    另一种选择

    声明 dynamic-module.ts

    import { NgModule, ANALYZE_FOR_ENTRY_COMPONENTS } from '@angular/core';
    
    @NgModule({})
    export class DynamicModule {
      static withComponents(components: any[]) {
        return {
          ngModule: DynamicModule,
          providers: [
            { 
              provide: ANALYZE_FOR_ENTRY_COMPONENTS,
              useValue: components,
              multi: true
            }
          ]
        }
      }
    }
    

    并将其导入 app.module.ts

    @NgModule({
      imports:      [ BrowserModule, DynamicModule.withComponents([TestPage]) ],
      declarations: [ MainComponent, TestPage ]
    })
    

    那么您的MainPage 组件可能如下所示:

    import { ViewChild, ViewContainerRef, ComponentFactoryResolver } from '@angular/core';
    @Component({
      selector: 'main-component',
      template: `
        <button (click)="putInMyHtml()">Insert component</button>
        <p>stuff</p>
        <div>
           <template #target></template> 
        </div>
      `
    })
    export class MainPage {
      @ViewChild('target', { read: ViewContainerRef }) target: ViewContainerRef;
      constructor(private cfr: ComponentFactoryResolver) {}
    
      putInMyHtml() {
        this.target.clear();
        let compFactory = this.cfr.resolveComponentFactory(TestPage);
    
        this.target.createComponent(compFactory);
      }
    }
    

    【讨论】:

    • 太棒了,我尝试了很多东西,这很令人头疼,Angular一直在变化
    • yurzui - 我尝试修改你的 plunker 以满足我的需要,但我的情况已经不同了我被卡住了 - 我分叉了你的 plunker - 你可以给我一些输入 - 因为你清楚地知道发生了什么.如果可以的话-谢谢。 stackoverflow.com/questions/49740046/…
    • 谢谢先生。一个非常好的和简单的解决方案。这是迄今为止我找到的最好的解决方案。
    猜你喜欢
    • 2017-06-17
    • 2019-12-08
    • 2017-01-17
    • 1970-01-01
    • 2017-07-07
    • 1970-01-01
    • 2023-01-12
    • 1970-01-01
    • 2018-05-31
    相关资源
    最近更新 更多