Angular – 单击菜单项后隐藏侧边栏菜单

前端之家收集整理的这篇文章主要介绍了Angular – 单击菜单项后隐藏侧边栏菜单前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我创建了一个侧边栏菜单,但是在点击菜单项后我无法隐藏菜单.我按照 https://blog.thecodecampus.de/angular-2-animate-creating-sliding-side-navigation/的例子

我是否需要从html上调用(点击)每个超链接的toggleMenu?如果是,如何从另一个组件调用app.component.ts中的方法

需要帮助请…

我正在使用Angular 4和bootstrap 4.

这是我的代码
app.component.html:

<button (click)="toggleMenu()" class="hamburger">
  <span>toggle menu</span>
</button>

<!--  <app-menu [@slideInOut]="menuState"></app-menu>  -->
<navigation-component [@slideInOut]="menuState"> </navigation-component>

<div class="container-fluid">
  <alert></alert>
  <router-outlet></router-outlet>
</div>

navigation.component.mobile.html:

<li><a routerLink="/home" routerLinkActive="active"> Home</a></li>
    <li>
        <a href="#submenu1" data-toggle="collapse">Alert</a>
        <ul id="submenu1" class="list-unstyled collapse">
            <li><a routerLink="/linesidemonitor" data-toggle="collapse" data-target=".navbar-collapse.show">IQS Alert</a></li>
        </ul>
    </li>
    <li routerLinkActive="active" [hidden]="!authenticated()">
        <a href="#submenu2" data-toggle="collapse">Configuration</a>
        <ul id="submenu2" class="list-unstyled collapse">
            <li><a routerLink="/contact" data-toggle="collapse" data-target=".navbar-collapse.show">Contacts</a></li>
            <li><a routerLink="/department" data-toggle="collapse" data-target=".navbar-collapse.show">Departments</a></li>
            <li><a routerLink="/notificationlevel">NotificationLevels</a></li>
            <li><a routerLink="/recipient">Recipients</a></li>
        </ul>
    </li>

app.component.ts:

@Component({
  selector: 'app-root',templateUrl: './'  + (window.innerWidth > 745 ? 
          'app.component.html' :
          'app.component.mobile.html'),styleUrls: ['./app.component.css'],animations: [
               trigger('slideInOut',[
                 state('in',style({
                   transform: 'translate3d(0,0)'
                 })),state('out',style({
                   transform: 'translate3d(100%,transition('in => out',animate('400ms ease-in-out')),transition('out => in',animate('400ms ease-in-out'))
               ]),]
})
  toggleMenu() {
      // 1-line if statement that toggles the value:
      this.menuState = this.menuState === 'out' ? 'in' : 'out';
  }

更新:

我试着调用toggleMenu().它正在工作,但页面再次加载.之前它曾经像AJAX调用(快速),但现在它就像是第一次加载新页面.所以我需要帮助,如何在http://parlaybuddy.razartech.com/no-auth完成它

https://jmouriz.github.io/angular-material-multilevel-menu/demo/demo.html#!/demo/views/item-1-1

https://stackblitz.com/edit/dynamic-nested-sidenav-menu

navigation.component.ts

toggleMenu() {
    this.toggleMenu();
}

HTML:

<ul id="submenu2" class="list-unstyled collapse">
            <li><a class="submenu" routerLink="/contact"  (click)="toggleMenu()">Contacts</a></li>
            <li><a class="submenu" routerLink="/department" (click)="toggleMenu()">Departments</a></li>

解:

正如Santosh所提到的,我在app.component.ts中添加了以下代码,它按预期工作.谢谢Santosh

constructor(private http: Http,private router: Router,public zone: NgZone) {
          router.events.subscribe( (event: Event) => {
              if (event instanceof NavigationStart) {
                  this.menuState = 'out';
              }

              if (event instanceof NavigationEnd) {
                  // Hide loading indicator
              }

              if (event instanceof NavigationError) {
                  // Hide loading indicator
                  // Present error to user
                  console.log(event.error);
              }
          });
  }
您可以在路由器事件中处理它,并在路由更改时将this.menuState设置为“out”.

一个相当不错的示例代码here

原文链接:https://www.f2er.com/angularjs/141277.html

猜你在找的Angularjs相关文章