我正在尝试使用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?
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)
UPD
| 归档时间: |
|
| 查看次数: |
2559 次 |
| 最近记录: |