简体   繁体   English

Angular 4路由带有无限/#/ url

[英]Angular 4 routing with infinite /#/ in url

Since some days I have this issue with Angular CLI and Routing. 有些日子我有Angular CLI和Routing这个问题。 If you point to the exact url, it seems work fine. 如果你指向确切的网址,它似乎工作正常。 When you try to redirect with routerLink, it writes infinite /#page in the url. 当您尝试使用routerLink重定向时,它会在URL中写入无限/#页面。 If you are on index and try to go to about page it writes http://baseurl/index#/about#/index#/index#/index#/index#/index#/index#/index#/ ... and goes on until finish the RAM :D 如果您在索引上并尝试转到关于页面,则会写入http:// baseurl / index#/ about#/ index#/ index#/ index#/ index#/ index#/ index#/ index#/ ...继续直到完成RAM:D

This problem is only in devmode with "ng serve" (but works with hashlocationstrategy). 此问题仅在具有“ng serve”的devmode中(但与hashlocationstrategy一起使用)。 In production mode seems work fine. 在生产模式似乎工作正常。

Here some codes. 这里有一些代码。

in index.html 在index.html中

<base href="/">

app.module app.module

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';
import { RouterModule } from '@angular/router';

import { AppComponent } from './app.component';

import { ConnectionService, ownerEnums, requestIdEnums } from './services/connection.service';
import { AuthService } from './services/auth.service';

import { AboutComponent } from './about/about.component';
import { ConnectComponent } from './connect/connect.component';
import { LayoutComponent } from './layout/layout.component';

import { HeaderComponent } from './layout/header/header.component';
import { BreadcrumbComponent } from './layout/breadcrumb/breadcrumb.component';
import { FooterComponent } from './layout/footer/footer.component';
import { IndexComponent } from './layout/index/index.component';

@NgModule({
  imports: [
    RouterModule.forRoot([
    { path: '', redirectTo: '/index', pathMatch: 'full' },
    { path: 'about', component: AboutComponent },
    { path: 'connect', component: ConnectComponent },
    { path: 'index', component: LayoutComponent }
    ]),
    BrowserModule,
    FormsModule,
    HttpModule
  ],
  declarations: [
    AppComponent,
    AboutComponent,
    ConnectComponent,
    LayoutComponent,
    HeaderComponent,
    BreadcrumbComponent,
    FooterComponent,
    IndexComponent
  ],
  providers: [ ConnectionService, AuthService ],
  bootstrap: [ AppComponent ]
})
export class AppModule { }

anchors with routerLink in about page 在页面中使用routerLink的锚点

    <div>
        <a class="navbar-brand" [routerLink]="['/about']"><i class="fa fa-info"></i></a>
        <a class="navbar-brand" [routerLink]="['/connect']"><i class="fa fa-plug"></i></a>
    </div>

In the @NgMoudle in your routing module add the {useHash:true} option. 在路由模块的@NgMoudle中添加{useHash:true}选项。 This will stop the problem, but I don't fully understand why. 这将阻止问题,但我不完全理解为什么。 That option is designed for compatibility with older browsers, but I am using latest Chrome. 该选项旨在与旧版浏览器兼容,但我使用的是最新版Chrome。 I have written other angular projects using same browser on same PC that don't have this problem, and my code with the problem seems identical. 我已经在没有这个问题的同一台PC上使用相同的浏览器编写了其他角度项目,并且我的问题代码看起来相同。 I write this not as a solution, but given no-one has answered this question, I hope my answer may serve as a 'clue' for someone better than me to explain what it happening and why 我写这不是一个解决方案,但鉴于没有人回答这个问题,我希望我的回答可以作为一个“线索”给一个比我更好的人解释它发生了什么以及为什么

@NgModule({
imports:[RouterModule.forRoot(appRoutes, {useHash: true})], 
exports:[RouterModule]
})

Change your router module to 将路由器模块更改为

{ path: 'about', component: AboutComponent },
{ path: 'connect', component: ConnectComponent },
{ path: 'index', component: LayoutComponent },
{ path: '', redirectTo: 'index', pathMatch: 'full'},
{path:'**',redirectTo:'index',pathMatch:'full'}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM