类型'ComponentFactory <spinnercomponent>'上不存在属性'then'

时间:2016-12-21 09:13:16

标签: angular

在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();
        }
    }
}

1 个答案:

答案 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);
}