【问题标题】:Angular 6 - RouterLink navigates but not changing URLAngular 6 - RouterLink 导航但不更改 URL
【发布时间】:2018-06-01 18:50:56
【问题描述】:

app.component.html

<nav>
 <a routerLink="/dashboard"></a>
 <a routerLink="/reports"></a>
<nav>
<main role="main" class="page-container">
  <router-outlet></router-outlet>
</main>

app-routing.module.ts

import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { DashboardComponent } from './dashboard/dashboard.component';
import { ReportsComponent } from './reports/reports.component';

const routes: Routes = [
  { path: 'dashboard', component: DashboardComponent },
  { path: 'reports', component: ReportsComponent },
  { path: '',
    redirectTo: '/dashboard',
    pathMatch: 'full'
  }];

@NgModule({
  imports: [RouterModule.forRoot(routes)],
  exports: [RouterModule]
})
export class AppRoutingModule { }

app.module.ts

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';

import { AppComponent } from './app.component';
import { AppRoutingModule } from './app-routing.module';
import { DashboardComponent } from './dashboard/dashboard.component';
import { ReportsComponent } from './reports/reports.component';


@NgModule({
  declarations: [
    AppComponent,
    DashboardComponent,
    ReportsComponent
  ],
  imports: [
    BrowserModule,
    AppRoutingModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule {

}

使用上面的代码,我看到点击导航链接时,可以导航到相应的组件,但由于某种原因,URL 没有得到更新。

不确定是否在这里遗漏了一些非常基本的东西。请帮忙。

【问题讨论】:

  • 它按预期工作,顺便说一句,您在哪里使用&lt;router-outlet&gt;stackblitz.com/edit/angular-nmv6bh
  • 您的代码似乎没有问题。 stackblitz.com/edit/angular-ru2wbj
  • @AnshumanJaiswal - 使用 更新了帖子。如果它按预期工作,我应该怎么做才能更改 URL?
  • @balachanderj to get the URL changed 是什么意思?它已经改变了。
  • 您要检查哪个 URL?浏览器网址?它工作正常。

标签: angular angular-cli angular-router routelink


【解决方案1】:

如果您正在运行混合应用程序(带有升级模块),请将 useHash 添加到路由器配置

imports: [RouterModule.forRoot(routes, { useHash: true, enableTracing: true })],

【讨论】:

  • 能否麻烦您解释一下这究竟是做什么的,以及为什么需要这样做?
  • “谨防巧合编程”!
猜你喜欢
  • 2017-06-29
  • 1970-01-01
  • 2017-12-16
  • 2018-06-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-14
  • 2021-03-30
相关资源
最近更新 更多