2017-08-21 77 views
3

這裏我有2個分量像CacadingComponent而LoginComponent我想消費在LoginComponentAngular2如何使用一個組件功能在另一個組件

CacadingComponent功能

CacadingComponent

export class CascadingComponent { 
    CountryList: Country[]; 
    constructor(private _CacadeService: CascadindService) {  
    } 
    GetCountry() { 
     return this._CacadeService.GetCountryList().subscribe((data) => this.CountryList = data); 
    } 

LoginComponent.ts

這裏我想調用GetCountry()函數在我的LoginComponent中

export class LoginComponent{ 
//Here How can I call 
} 
+0

您可以使用一個服務,如果你想擁有共享功能等 此外,您還可以使用組件視圖中的組件。 –

+0

是@Dawid但要求是不同的 –

回答

2
export class LoginComponent implements OnInit{ 
    @ViewChild(CascadingComponent) cascadingComponent: CascadingComponent ; 
    ngOnInit(){ 
    this.cascading.GetCountry();} 
} 
3

您可以通過CascadingComponent類的對象訪問方法,如下圖所示:

export class LoginComponent implements OnInit{ 

     cascadingComponent = new CascadingComponent(); 

     ngOnInit(){ 
     this.cascadingComponent.GetCountry(); 
     } 
} 
相關問題