【问题标题】:angular route fragment replacing complete URL path角度路由片段替换完整的 URL 路径
【发布时间】:2020-05-23 16:37:01
【问题描述】:

我正在尝试使用 ng-bootstrap 将 Angular 9 路由片段用于导航选项卡,并引用了此 ng-bootstrap documentation。问题在于添加点击选项卡它正在替换整个 URL 路径,例如单击选项卡后路径应该像https://www.mypleaks.com/home#International,但它正在形成https://www.mypleaks.com/#International,它完全取代了/home路径

stackblitz view link 和 stackblitz edit link

HTML

<ul ngbNav [activeId]="route.fragment | async" class="nav-tabs justify-content-center container-lg">
    <li [ngbNavItem]="flatTab.name" *ngFor="let flatTab of flatTabs">
        <a ngbNavLink routerLink="." [fragment]="flatTab.name"
        [class.active]="flatTab.active" (click)="activateTab(flatTab)">{{ flatTab.name }}</a>
    </li>
</ul>

组件

import { Component, OnInit } from '@angular/core';
import { isPlatformBrowser } from '@angular/common';
import { ActivatedRoute } from '@angular/router';

import { TabService } from '../../services/tab.service';
import { FlatTab } from '../../modals/flat/flat-tab';

@Component({
  selector: 'app-tabs',
  templateUrl: './tabs.component.html',
  styleUrls: ['./tabs.component.scss']
})
export class TabsComponent implements OnInit, AfterViewInit {

  public flatTabs : FlatTab[];
  constructor(public tabService: TabService, public route: ActivatedRoute) { }

  ngOnInit(): void {
    this.tabService.getTabs().subscribe(resp => {
      this.flatTabs = { ...resp.body }.flatTabs;
      this.route.fragment.subscribe((name: string)=>{
        let isActiveSet = false;
        this.flatTabs.forEach(flatTab => {
          if(name == flatTab.name){
            flatTab.active = true;
          }else{
            flatTab.active = false;
          }
          if (!name && !isActiveSet) {
            isActiveSet = true;
            flatTab.active = true;
          }else if (!name && isActiveSet) {
            flatTab.active = false;
          }
        });
      });
    });
  }
}

平面标签

export class FlatTab {
    tabId: number;
    name: string;
    active: boolean;
}

提前致谢

【问题讨论】:

    标签: angular angular-routing angular-router


    【解决方案1】:

    你的 app-routing.module.ts 应该有以下代码:-

    import { NgModule } from '@angular/core';
    import { Routes, RouterModule } from '@angular/router';
    import { HomeComponent } from './home/home.component';
    
    
    const routes: Routes = [
      {path: '', pathMatch: 'full', redirectTo: 'home'},
      {path: 'home', component: HomeComponent}
      ];
    
    @NgModule({
      imports: [RouterModule.forRoot(routes, {
        initialNavigation: 'enabled'
      })],
      exports: [RouterModule]
    })
    export class AppRoutingModule { }
    

    您的 home.component.html 应该有以下代码:-

    <ul ngbNav [activeId]="route.fragment | async" class="nav-tabs justify-content-center container-lg">
        <li [ngbNavItem]="flatTab.name" *ngFor="let flatTab of flatTabs">
            <a ngbNavLink routerLink="home" [fragment]="flatTab.name"
             [class.active]="flatTab.active" (click)="activateTab(flatTab)">{{ flatTab.name }}</a>
        </li>
    </ul>
    

    下面的堆栈闪电战:-

    https://stackblitz.com/edit/desh-angular-fragment-x3kj4w

    【讨论】:

    猜你喜欢
    • 2018-11-28
    • 2014-12-14
    • 1970-01-01
    • 2019-02-08
    • 2017-03-16
    • 1970-01-01
    • 2014-07-13
    • 2012-07-26
    相关资源
    最近更新 更多