在coreUI中使用app-sidebar-nav时如何操作routerLink?

时间:2019-03-13 12:26:46

标签: angular typescript core-ui

我正在使用CoreUI的app-sidebar-nav(链接here)重定向到应用程序的各个部分。但是,子路由会附加到当前网址,而不是附加到应用程序的根目录。

我在解决here的同一应用程序中的工具栏也遇到了类似的问题(经过here的一些修改后),但是在这种情况下,我使用的是CoreUI模板,并且想要知道是否有一种方法可以像在工具栏中一样来操作routerLink。 this可能对确定routerLink所在的位置有帮助

这是我的代码:

component.ts

import { Component, OnInit } from '@angular/core';
import { navItems } from 'src/app/views/core/side-nav/_nav';
/**
* This component renders the UI of the side-nav component.
*/
@Component({
  selector: 'app-side-nav',
  templateUrl: './side-nav.component.html',
  styleUrls: ['./side-nav.component.scss']
})
export class SideNavComponent implements OnInit {
  /**
  * The constructor initializes the navItems meta.
  */
  constructor(private nav: navItems) { }
  /**
  * This property comprises the navItems meta.
  */
  public navItems = this.nav.exportData();
  /**
  * This flag indicates wether the sidebar is minimized or not.
  */
  public sidebarMinimized = true;
  /**
  * This property is an eventlistener.
  */
  private changes: MutationObserver;
  /**
  * This property contains the document body.
  */
  public element: HTMLElement = document.body;

  /**
  * This method observes the changes in the DOM, and minimizes and maximizes the
  * side nav accordingly.
  */
  ngOnInit() {
    this.changes = new MutationObserver((mutations) => {
      this.sidebarMinimized = document.body.classList.contains('sidebar-minimized');
    });
    this.changes.observe(<Element>this.element, {
      attributes: true
    });
  }

}

导航ts文件

import { TranslatePipe } from 'src/app/pipes/translate/translate.pipe';
import { Injectable } from '@angular/core';
import { DynamicRoutingService } from 'src/app/services/dynamic-routing/dynamic-routing.service';
import { Router } from '@angular/router';
import { DummyComponent } from 'src/app/views/dummy/dummy.component';
import { LogoutComponent } from 'src/app/views/logout/logout.component';

@Injectable({
  providedIn: 'root'
})

/**
* The purpose of this class is to provide an object containing the translated names
* for the categories in the nav bar, based on the translation file found in assets/i18n
*
* @class navItems
*/
export class navItems {

  /**
  * Object containing the translated names and their respective icons
  * @property {array} navData
  */
  navData: Array<{ name: string, url: string, icon: string }>;

  /**
  * constructor for toolbar component is responsible for initializing translatePipe, dynamic routing and router,
  * as well as adding routes dynamically to the router and the dynamicRouting service
  * @param translate
  * @param router
  * @param dynamicRouting
  *
  */
  constructor(private translate: TranslatePipe, private router: Router, private dynamicRouting: DynamicRoutingService) {
    this.router.config.unshift(
      { path: 'knowledge-base', component: DummyComponent, pathMatch: "full" },
      { path: 'home', component: DummyComponent },
      { path: 'profile', component: DummyComponent },
      { path: 'settings', component: DummyComponent },
      { path: 'logout', component: LogoutComponent}
    );
    this.dynamicRouting.addItem({ text: "home", path: "home", icon: "icon-drop" });
    this.dynamicRouting.addItem({ text: "knowledge_base", path: "knowledge-base", icon: "icon-pencil" });
    this.dynamicRouting.addItem({ text: "profile", path: "profile", icon: "icon-puzzle" });
    this.dynamicRouting.addItem({ text: "settings", path: "settings", icon: "icon-cursor" });
    this.dynamicRouting.addItem({ text: "logout", path: "logout", icon: "icon-arrow-left-circle" });
  }

  /**
  * When called this method sets the required data into the navData object
  * @param
  * @method exportData
  * @return
  */
  exportData() {
    this.navData = [];
    let rawData = this.dynamicRouting.getLinks();
    let self = this;
    rawData.forEach(function(data) {
      let text = self.translate.transform("generic[nav_bar][categories][" + data.text + "][label]");
      self.navData.push({ name: text, url: data.path, icon: data.icon });
    });
    return this.navData;
  }
}

component.html

<app-sidebar [fixed]="true" [display]="'lg'">
  <app-sidebar-nav [navItems]="navItems" [perfectScrollbar] [disabled]="sidebarMinimized"></app-sidebar-nav>
  <app-sidebar-minimizer class="custom-padding-bottom"></app-sidebar-minimizer>
</app-sidebar>

0 个答案:

没有答案