abc.component.ts
import...
...
...
@component({ ... })
export class Abc {
public foo(){
console.log("execute foo in UserComponent's xyz() ");
}
}
user.component.ts
import...
...
...
import {Abc} from '../utils/abc.component';
@Component({
selector: 'user',
templateUrl: 'app/user/user.html',
providers: [HTTP_PROVIDERS],
directives: [Abc]
})
export class UserComponent {
xyz(){
Abc.foo(); //--> ?
}
}
我在user.component.ts中导入了Abc模块。如何在UserComponent类方法中调用Abc.foo()?
答案 0 :(得分:0)
您可以使用官方文档Angula2中的Parent calls a ViewChild场景
父组件
import {Component, ViewChild} from '@angular/core'
@Component({
selector: 'my-app',
template: `
<div>
<abc></abc>
</div>`
})
export class App {
@ViewChild(ABC)
private abc:ABC;
ngAfterViewInit() {
this.abc.foo();
}
}
对应的plunker示例在这里https://plnkr.co/edit/VPsMTtJ29XBWr7SCqbzA?p=preview