【发布时间】:2016-09-20 03:33:52
【问题描述】:
我遇到了打字错误
错误 TS2339:类型“{}”上不存在属性“close”
我遇到问题的代码:-
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);
}
}
谁能帮帮我?我是 Angular 2 和打字稿的新手。我的编码可能是错误的。如果我的编码错误,请纠正我。
PS:即使 typescript 抛出此错误,此代码仍可按我的意愿工作(在开发版本中)。但不能做产品构建
谢谢
【问题讨论】:
标签: angular typescript