angular – 如何在其他组件中排除app.component.html的代码

前端之家收集整理的这篇文章主要介绍了angular – 如何在其他组件中排除app.component.html的代码前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
app.component.html是我们的默认组件,其中添加了一个菜单,但我不希望将其包含在我的其他组件中.

我该怎么做 ?

问题是当我加载newsection.component.html时,它会在顶部显示app.component.html,如下所示

enter image description here

Please guide how can i restrict it so that it doesnt load above editsection.component.html

解决方法

你可以结合Dhyey和Hamed Baatour的回答.

首先,为菜单创建一个单独的组件(例如menu.component.ts)并将所有代码放在那里.

然后修改你的app.component.ts,使它看起来像这样:

<menu-component *ngIf="showMenu"></menu-component>
<router-outlet></router-outlet>

重要提示:现在您必须在app.component.ts中设置showMenu,而不是在newsection.component.ts中设置.

您可以通过检查请求的URL来执行此操作.只需在您的app.component.ts中注入路由器,如下所示:

constructor(router:Router) {
router.events.forEach((event) => {
    if(event instanceof NavigationStart) {
        this.showMenu = event.url !== "/newsection";
    }
  });
}

如果请求带有/ newsection的路由,则不应显示菜单.

猜你在找的Angularjs相关文章