2017-03-14 35 views
0

我使用子路由。我得到了主動路由中的父組件。但是我不知道如何使用它來調用方法。我想要投射組件以便使用它

constructor(public router : Router, public route : ActivatedRoute) { } 

ngOnInit() { 
    let comp = (this.route.parent.component as Type<RegisterComponent>); 
    console.log(comp); 
} 

回答

0

兩種不同的方式:

let comp = <RegisterComponent> this.route.parent.component; 

或者

let comp = this.route.parent.component as RegisterComponent; 
+0

thaniks爲你解答。當我這樣做時,編輯器顯示一個錯誤:type string |類型不能轉換爲類型RegisterComponent。類型''與typr RegisterComponent不具有可比性。爲什麼? – drtapha

相關問題