Angular2 如何在另一个组件中使用一个组件函数
Angular2 How can i Use One Component function In Another Component
这里我有 2 个组件,例如 CacadingComponent 和 LoginComponent 我想在 LoginComponent 中使用 CacadingComponent 函数
CacadingComponent
export class CascadingComponent {
CountryList: Country[];
constructor(private _CacadeService: CascadindService) {
}
GetCountry() {
return this._CacadeService.GetCountryList().subscribe((data) => this.CountryList = data);
}
LoginComponent.ts
这里我想在我的 LoginComponent 中调用 GetCountry() 函数
export class LoginComponent{
//Here How can I call
}
export class LoginComponent implements OnInit{
@ViewChild(CascadingComponent) cascadingComponent: CascadingComponent ;
ngOnInit(){
this.cascading.GetCountry();}
}
您可以通过创建 class CascadingComponent
的对象来访问方法,如下所示:
export class LoginComponent implements OnInit{
cascadingComponent = new CascadingComponent();
ngOnInit(){
this.cascadingComponent.GetCountry();
}
}
这里我有 2 个组件,例如 CacadingComponent 和 LoginComponent 我想在 LoginComponent 中使用 CacadingComponent 函数
CacadingComponent
export class CascadingComponent {
CountryList: Country[];
constructor(private _CacadeService: CascadindService) {
}
GetCountry() {
return this._CacadeService.GetCountryList().subscribe((data) => this.CountryList = data);
}
LoginComponent.ts
这里我想在我的 LoginComponent 中调用 GetCountry() 函数
export class LoginComponent{
//Here How can I call
}
export class LoginComponent implements OnInit{
@ViewChild(CascadingComponent) cascadingComponent: CascadingComponent ;
ngOnInit(){
this.cascading.GetCountry();}
}
您可以通过创建 class CascadingComponent
的对象来访问方法,如下所示:
export class LoginComponent implements OnInit{
cascadingComponent = new CascadingComponent();
ngOnInit(){
this.cascadingComponent.GetCountry();
}
}