DynamicComponentLoader.loadIntoLocation不是Angular2中的函数错误

won*_*rld 2 angular

我正在尝试使用DynamicComponentLoader,示例代码如下:

import {Component, View, bootstrap,  OnInit, DynamicComponentLoader} from 'angular2';

...

DynamicComponentLoader.loadIntoLocation(PersonsComponent, itemElement);
Run Code Online (Sandbox Code Playgroud)

当我运行应用程序时,我收到错误:

DynamicComponentLoader.loadIntoLocation不是函数

如何在使用类的ES6 JavaScript中使用DynamicComponentLoader.loadIntoLocation?

ale*_*ods 5

DynamicComponentLoader上课.它没有任何静态方法loadIntoLocation.但是这个类的实例有它.您必须DynamicComponentLoader使用依赖注入进行实例化:

import { Component, View, DynamicComponentLoader, ElementRef } from 'angular2/angular2'

@Component({
  selector: 'dc'
})
@View({
  template: '<b>Some template</b>'
})
class DynamicComponent {}

@Component({
  selector: 'my-app'
})
@View({
  template: '<div #container></div>'
})
export class App {

  constructor(
    dynamicComponentLoader: DynamicComponentLoader, 
    elementRef: ElementRef
  ) {
    dynamicComponentLoader.loadIntoLocation(DynamicComponent, elementRef, 'container');
  }
}
Run Code Online (Sandbox Code Playgroud)

看到这个plunker

UPD

ES6怎么样,我在这里回答