1
我面臨着一個打字稿錯誤打字稿錯誤 - TS2339上ComponentRef
錯誤TS2339:房產 '關閉' 不會對類型存在 '{}'
代碼,我面對的問題: -
home.directive.ts
import { Directive, ComponentFactoryResolver, ComponentRef, ViewContainerRef } from '@angular/core';
@Directive({
selector: '[child]'
})
export class HomeDirective {
constructor(private viewContainer: ViewContainerRef,
private componentFactoryResolver: ComponentFactoryResolver) {
}
openComp(component:any) : ComponentRef<any> {
this.viewContainer.clear();
let componentFactory =
this.componentFactoryResolver.resolveComponentFactory(component);
let componentRef = this.viewContainer.createComponent(componentFactory);
componentRef.instance.close.subscribe(() => {
//do something
});
return componentRef;
}
}
個child.component.ts
import { Component, EventEmitter } from '@angular/core';
import { HomeService } from '../home.service';
@Component({
selector: 'child-component',
providers: [HomeService],
template: `<h3>child-component</h3>`
})
export class ChildComponent {
close = new EventEmitter();
constructor() {
}
ngOnDestroy() {
this.close.emit('closed');
}
}
,我調用openComp()
home.component.ts
import { Component ,ViewChild } from '@angular/core';
import { HomeService } from './home.service';
import { HomeDirective } from './home.directive';
import { ChildComponent } from './child/index';
@Component({
moduleId: module.id,
selector: 'sd-home',
providers: [HomeService],
templateUrl: 'home.component.html',
styleUrls: ['home.component.css'],
entryComponents: [ChildComponent],
})
export class HomeComponent {
@ViewChild(HomeDirective) homeDirective: HomeDirective;
constructor(private _homeService: HomeService) {
}
openChild() {
this.homeDirective.openComp(ChildComponent);
}
}
誰能幫助我嗎?我是角2和打字稿的新手。我的編碼可能是錯的。如果我的編碼錯誤,請糾正我。 PS:儘管打字稿引發這個錯誤,但是這段代碼正如我想要的那樣工作(在開發版本中)。但不能做督促建立
感謝
它的工作非常感謝.... – i7326