2016-12-14 32 views
0

我想將角度2應用程序集成到使用freemarker的現有應用程序中。使用路由器在根組件中顯示組件

我想使用路由器通過給根組件的名稱顯示正確的組件。

<my-app displayComponent="myComponent">Loading...</my-app> 

我可以檢索根組件中的componentToDisplay屬性,但當我嘗試導航到組件myComponent時,它不起作用。

export class AppComponent implements OnInit { 

    displayComponent: string; 

    constructor(private router: Router, private elm: ElementRef) { 
     this.displayComponent = elm.nativeElement.getAttribute('displayComponent'); 
    } 

    ngOnInit(): void { 
     this.router.navigate([this.displayComponent]); 
    } 
} 

任何想法?

感謝

回答

0

你有沒有添加的路由邏輯在Routes

{ 
    path: 'component/path', 
    component: ComponentName 
} 
+0

是,萬一我試着撥打導航功能與子組件在相同的成分,它的工作原理。所以問題在於導航功能在根組件中不起作用。 –

0

有一種變通方法現在:

ngOnInit(): void { 
    setTimeout(() => this.router.navigate([this.displayComponent])); 
} 
相關問題