【问题标题】:http://localhost:4200/#/dashboard, extra '#' in url Angular 5http://localhost:4200/#/dashboard,额外的 '#' 在 url Angular 5
【发布时间】:2018-08-15 01:09:15
【问题描述】:

我在 Angular 5 项目中遇到了这个问题。所有使用 localhost 的 URL,甚至在托管时,在域名后跟一个“#”。找不到原因。

app-routing.module.ts 其他组件有子路由

const appRoutes: Routes = [
  { path: 'login', component: LoginComponent},
  { path: 'new-vendor-registration', component: NewVendorComponent},
  { path: 'new-vendor-card-add', component: NewVendorCardComponent },
  { path: 'forgot-password', component: ForgotPasswordComponent},
  {
    path: '',
    redirectTo: 'dashboard',
    pathMatch: 'full',
  },
  { path: '', component: DashboardLayoutComponent, data: { title: '' }, children: DASHBOARD_ROUTES },
  { path: '', component: DashboardLayoutComponent, data: { title: '' }, children: USER_ROUTES, canActivate: [AuthGuard] },
  { path: '', component: DashboardLayoutComponent, data: { title: '' }, children: OFFER_ROUTES, canActivate: [AuthGuard] },
  { path: '', component: DashboardLayoutComponent, data: { title: '' }, children: PROMOTION_ROUTES, canActivate: [AuthGuard] }
  // { path: '', component: TwoColumnsLayoutComponent, data: { title: '' }, children: TWO_COLUMN_ROUTES, canActivate: [AuthGuard] },
];

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

app.module.ts

@NgModule({
    declarations: [
        AppComponent,
        TwoColumnsLayoutComponent,
        DashboardLayoutComponent,
        LoginComponent,
        NewVendorComponent,
        NewVendorCardComponent,
        ForgotPasswordComponent
    ],
    imports: [
        BrowserAnimationsModule,
        NgxDatatableModule,
        HttpClientModule,
        FormsModule,
        AppRoutingModule,
        SharedModule,
        NgbModule.forRoot(),
        AngularFireModule.initializeApp(firebaseConfig),
        AngularFireDatabaseModule,
        AngularFireAuthModule,
        MyDatePickerModule,
        TextMaskModule
    ],
    providers: [
        {provide: LocationStrategy, useClass: HashLocationStrategy},
        AuthService,
        AuthGuard,
        VendorService,
        SalesrepService,
        PaymentService,
        SubscriptionService,
        OfferService,
        PromotionService,
        WeatherService,
        Broadcaster
    ],
    bootstrap: [AppComponent]
})
export class AppModule {}

非常感谢任何帮助。

【问题讨论】:

  • angular.io/api/common/LocationStrategy 显示你的 app.module.ts
  • 你能显示你的navbar html 代码吗?我猜你的anchor 标签href="#" 正在制造问题。将其更改为“javascript:void(0)”
  • 在 index.html 中验证您的基本路径,它应该类似于 没有哈希。
  • 我用 app.module.ts @ngmodule 更新了帖子。基本 href 设置为“/”。还是一样

标签: angular routes


【解决方案1】:

这是您看到哈希的原因

{provide: LocationStrategy, useClass: HashLocationStrategy}

在此处了解更多信息:

HashLocationStrategy

你很可能想要这个:

PathLocationStrategy

根据Description

如果您使用 PathLocationStrategy,则必须提供 APP_BASE_HREF 或向文档添加基本元素。此 URL 前缀将是 在生成和识别 URL 时保留。

例如,如果您提供 '/my/app' 的 APP_BASE_HREF 并调用 location.go('/foo'),浏览器的URL会变成 example.com/my/app/foo.

同样,如果您添加到文档并调用 location.go('/foo'),浏览器的URL会变成 example.com/my/app/foo.

【讨论】:

  • 我认为这是因为托管应用程序时出现问题而添加的。如果我完全删除它,它仍然可以托管吗?
  • 通常删除“#”会干扰路由与服务器的工作方式,因此添加#对于角度应用程序(或其他框架)效果更好。没有它你仍然可以让它工作,但它需要更多的设置
  • 感谢 Ric 的快速回复
猜你喜欢
  • 1970-01-01
  • 2018-10-27
  • 2019-09-02
  • 1970-01-01
  • 2021-01-09
  • 2016-11-28
  • 2018-11-15
  • 2016-04-07
  • 2021-06-28
相关资源
最近更新 更多