在Angular版本2.2.0
下面,我在线下错误
Plunker Link,
http://plnkr.co/edit/f6GBBQan7z4I9K1qBZOi?p=preview
spinnerRef.then((factory: ComponentFactory<any>) => {
错误,属性''''在'ComponentFactory'类型上不存在
这可能是什么原因和解决方案,请建议。谢谢!
import { Injectable, ApplicationRef, ViewContainerRef, Component, ComponentRef, ComponentFactoryResolver, ComponentFactory, ViewChild } from '@angular/core';
import { SpinnerComponent } from '../components/blockui/blockui.component';
@Injectable()
export class SpinnerService {
spinnerComp: ComponentRef<any>;
constructor(private _appRef: ApplicationRef, private _resolver: ComponentFactoryResolver) {
}
public start() {
let elementRef: ViewContainerRef = (<any>this._appRef)['_rootComponents'][0].location;
return this.startInside(elementRef, null);
}
public startInside(elementRef: ViewContainerRef, anchorName: string) {
let spinnerRef = this._resolver.resolveComponentFactory(SpinnerComponent);
spinnerRef.then((factory: ComponentFactory<any>) => {
this.spinnerComp = elementRef.createComponent(factory)
});
}
public stop() {
if (this.spinnerComp) {
this.spinnerComp.destroy();
}
}
}
答案 0 :(得分:1)
resolveComponentFactory(component: Type<T>) : ComponentFactory<T>
返回ComponentFactory<T>
而不是Promise<ComponentFactory<T>>
- 它不是异步。
https://angular.io/docs/ts/latest/api/core/index/ComponentFactoryResolver-class.html
public startInside(elementRef: ViewContainerRef, anchorName: string) {
let factory = this._resolver.resolveComponentFactory(SpinnerComponent);
this.spinnerComp = elementRef.createComponent(factory);
}