2 回答

TA貢獻1966條經驗 獲得超4個贊
上述解決方案肯定會起作用,但理想情況下,您應該使用 Angular 路由器。這就是我認為你在這里想要實現的目標。
以下是您需要實施的更改:
someRootComponent.html
<app-new-post></app-new-post>
<app-filter-forum></app-filter-forum>
<router-outlet></router-outlet>
此時你可以擺脫ComponentA和componentB。您的路線如下所示:
const routes: Routes = [
{ path: 'view-topic', component: ViewTopicComponent },
{ path: 'sub-topic', component: SubTopicComponent },
{ path: '**', component: PageNotFoundComponent }, // Wildcard route for a 404 page
];
您最終會遇到的問題是管理狀態。有很多方法可以處理這個問題。如果您想支持深度鏈接,那么我建議您這樣做:
const routes: Routes = [
{ path: 'view-topic/:topicId', component: ViewTopicComponent },
{ path: 'sub-topic/:subTopicId', component: SubTopicComponent },
{ path: '**', component: PageNotFoundComponent }, // Wildcard route for a 404 page
];
這是鏈接到主題的方式:
<a [routerLink]="['/view-topic', topic.id]">
Angular 路由器文檔非常好,可以在這里找到: Angular Router Docs

TA貢獻1869條經驗 獲得超4個贊
您可以使用 ngIf 檢查以顯示不同的 dom
ComponentA.html
<app-new-post></app-new-post>
<app-filter-forum></app-filter-forum>
<app-view-topic [topicData]="viewContent" *ngIf="!showBComponent"
(showSubTopic)="onShowSubTopic($event)"></app-view-topic>
<app-sub-topic *ngIf="showBComponent "></app-sub-topic>
ComponentA.ts
let showBComponent: boolean = false;
onShowSubTopic(e: any): void {
this.showBComponent = true;
}
添加回答
舉報