【问题标题】:use dynamic component loader in angular2?在angular2中使用动态组件加载器?
【发布时间】:2016-03-03 11:01:26
【问题描述】:

我使用 typeScript 使用 angular2 的 dynamicComponentloader 创建了以下应用程序。 但是我的子组件没有被渲染。

我的组件:

import {Component, Directive, ElementRef, Renderer,DynamicComponentLoader} from 'angular2/core';
import {RouteConfig, ROUTER_DIRECTIVES} from 'angular2/router';
import {DynamicBody1Component} from './ICICI-DYNAMIC-BODY-1.component';

@Directive({
  selector: '[x-large]'
})
export class XLarge {
  constructor(element: ElementRef, renderer: Renderer) {
    // we must interact with the dom through Renderer for webworker/server to see the changes
    renderer.setElementStyle(element.nativeElement, 'fontSize', 'x-large');
  }
}


@Component({
  selector: 'app',
  directives: [
    ...ROUTER_DIRECTIVES,
    XLarge
  ],
  styles: [`
    .router-link-active {
      background-color: lightgray;
     }`
    ],
  template: `
    <div>    
        <div>
            <span x-large>Hello, {{ name }}!</span>
        </div>
        <div>
            <div #container></div>
        </div>
      </div>
      `
 })
export class App {
  name: string = 'Angular 2';
  public dynamicComponentLoader: DynamicComponentLoader;  
  constructor(dynamicComponentLoader: DynamicComponentLoader, private     elementRef: ElementRef) {}

  ngOnInit() {
    this.dynamicComponentLoader.loadIntoLocation(DynamicBody1Component, this.elementRef, 'container');

  }
}

这里有什么问题吗? 提前致谢。

【问题讨论】:

    标签: angular typescript dynamic-loading


    【解决方案1】:

    有关最新示例,请参阅Angular 2 dynamic tabs with user-click chosen components

    原创

    您不能再在constructor() 中使用dynamicComponentLoader。移至ngOnInit()

    import {Component, Directive, ElementRef, Renderer,DynamicComponentLoader} from 'angular2/core';
    import {RouteConfig, ROUTER_DIRECTIVES} from 'angular2/router';
    import {DynamicBody1Component} from './DYNAMIC-BODY-1.component';
    
    @Directive({
      selector: '[x-large]'
      host: {
        '[style.fontSize]': 'x-large',
      }
    })
    export class XLarge {
    }
    
    
    @Component({
      selector: 'app',
      directives: [
        ...ROUTER_DIRECTIVES,
        XLarge
      ],
      styles: [`
        .router-link-active {
          background-color: lightgray;
        }
      `],
      template: `
      <div>    
        <div>
          <span x-large>Hello, {{ name }}!</span>
        </div>
        <div>
            <div #container></div>
        </div>
      </div>
      `
    })
    export class App {
      name: string = 'Angular 2';
      constructor(private dynamicComponentLoader: DynamicComponentLoader, private elementRef: ElementRef) {}
    
      ngOnInit() {
        this.dynamicComponentLoader.loadIntoLocation(DynamicBody1Component, this.elementRef, 'container');
    
      }
    }
    

    【讨论】:

    • 我试过你的解决方案,现在我收到了这个错误;EXCEPTION: TypeError: Cannot read property 'loadIntoLocation' of undefined in [null] ORIGINAL EXCEPTION: TypeError: Cannot read property 'loadIntoLocation' of undefined...查看有问题的更新代码
    • 对不起,忘记在构造函数前删除App中的一行。
    • @gunter.its 仍然无法正常工作。有关详细信息,请参阅快照。我正在使用通用启动器进行服务器端渲染。没有显示错误。但我的组件也没有在浏览器中加载。
    • 但是您没有收到错误消息?我还更改了XLarge。为此,您不需要ElementRef。但这并不能解决问题。
    • @guner 如果你可以参考快照,你可以看到控制台没有错误,但是组件没有被加载。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-25
    • 2016-08-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多