有条件的成角度路线分量

时间:2018-07-02 07:45:48

标签: angular routing

我在“ feed”模块的应用程序中有一个路由。在摘要中,我有两种类型的帖子,我需要打开此帖子并通过直接链接显示完整信息,如何根据情况打开合适的组件,或者如何解决此问题?

const feed_routes: Routes = [
    { path: '', component: ParentComponent,
        children: [
            { path: ':slug', component: FirstComponent },
            { path: ':slug', component: SecondComponent },
        ]
    }
];

1 个答案:

答案 0 :(得分:2)

使用通用组件呈现所有帖子。并在模板中像这样分别渲染两种类型的帖子。

在component.ts

@Component({
  selector: 'app-post-container',
  styleUrls: ['./app-post-container.component.scss'],
  templateUrl: './app-posts-container.component.html'
})
export class PostsContainerComponent {
  @input()
  public type;
}

在component.html

<div>
  <first-component *ngIf="type === 'conditionOne'"></first-component>
  <second-component *ngIf="type === 'conditionTwo'"></second-component>
</div>