在您的侧边栏组件中,您可以使用Angular Router来控制侧边栏的可见性。要做到这一点,您需要在侧边栏组件中监听路由更改事件,并判断当前路由是否是您想要隐藏侧边栏的路由。一旦路由匹配,您可以将侧边栏设置为不可见。
可以按照以下步骤执行:
import { Component } from '@angular/core'; import { Router } from '@angular/router';
@Component({
selector: 'sidebar',
templateUrl: './sidebar.component.html'
})
export class SidebarComponent { constructor(private router: Router) {} }
ngOnInit() { this.router.events.subscribe((val) => { if (this.router.url === '/my-page') { this.hideSidebar(); } }); }
hideSidebar() { // set sidebar visibility to false }
通过这种方法,您可以在 Angular 2 应用程序中隐藏侧边栏菜单。在代码示例中,我们使用 “/my-page” 路由路径来隐藏侧边栏。您可以替换路由路径,以适应您的电影和情况。