美文网首页
左侧导航组件刷新

左侧导航组件刷新

作者: 环零弦 | 来源:发表于2018-04-19 17:31 被阅读0次

左侧导航的模板是这样的:

<nav class="sidebar-nav" *ngIf="showFlag">
  <ul class="nav">
    <ng-template ngFor let-navitem [ngForOf]="navigation_">
      <li *ngIf="isDivider(navitem)" class="nav-divider"></li>
      <ng-template [ngIf]="isTitle(navitem)&&hasPrivilege(navitem['pcode'])">
        <app-sidebar-nav-title [title]='navitem'></app-sidebar-nav-title>
        <ng-template ngFor let-child [ngForOf]="navitem.children">
          <app-sidebar-nav-item *ngIf="hasPrivilege(child['pcode'])" [item]='child'></app-sidebar-nav-item>
        </ng-template>
      </ng-template>
    </ng-template>
  </ul>
</nav>

其中 sidebar-nav 作为指令的选择器,还被进行了如下操作:

const nativeElement: HTMLElement = this.el.nativeElement;
const parentElement: HTMLElement = nativeElement.parentElement;
// move all children out of the element
while (nativeElement.firstChild) {
  parentElement.insertBefore(nativeElement.firstChild, nativeElement);
}
// remove the empty element(the host)
parentElement.removeChild(nativeElement);

这样一来,根本就没有办法通过绑定的 navigation_ 的变化来更新视图,只能通过刷新整个组件。于是解决方案如下:
在模板下面

public ngOnInit() {
  this.navUpdateService.getCurrentNav().subscribe(_navigation => {
    this.navigation_ = _navigation;
    this.privilegeCodeSet = new Set(JSON.parse(this.ls.get('privilegeCode')) || []);
    this.showFlag = false;
    setTimeout(() => {
      this.showFlag = true;
    }, 0);
  });
  this.navUpdateService.setCurrentNav();
}

当其他组件中要触发更新左侧导航时,只有触发相应 rxjs 更新就好了。

相关文章

网友评论

      本文标题:左侧导航组件刷新

      本文链接:https://www.haomeiwen.com/subject/owawkftx.html