app.component.html是我们的默认组件,其中添加了一个菜单,但我不希望将其包含在我的其他组件中.
我该怎么做 ?
问题是当我加载newsection.component.html时,它会在顶部显示app.component.html,如下所示
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"; } }); }