簡體   English   中英

刪除 URL Angular 11 中的哈希(#)

[英]Remove hash(#) in URL Angular 11

我想把URL中的#去掉,但是去掉的時候,部署到服務器上就會出現問題。 當頁面刷新時,將獲得 404 狀態。

例子

https://一個/用戶/(作品)

https://一個/用戶/1(不工作)

應用程序路由.module.ts

    @NgModule({
  imports: [RouterModule.forRoot(routes)],
  providers: [
    {provide: LocationStrategy, useClass: PathLocationStrategy} 
  ],
  exports: [RouterModule]
})

app.module.ts

......
providers: [ Location, {provide: LocationStrategy, useClass: PathLocationStrategy}]

請告訴我我該怎么做。

PathLocationStrategy 是 Angular Routing 的默認定位策略,它應該可以解決你的 hash(#) 問題。

您的代碼中沒有錯誤,請仔細檢查以下幾點

  1. RouterModule.forRoot(routes, { useHash: true }) //use Hash should not be there
  2. providers: [ // 下面的行是可選的,因為默認 LocationStrategy 是 PathLocationStrategy {provide: LocationStrategy, useClass: PathLocationStrategy} ]

如果您在部署時僅在服務器中遇到問題,請檢查服務器中的入口點配置。 它應該是 index.html 文件。

注意:使用 PathLocationStrategy 時,您需要配置您的 web 服務器為所有請求的位置提供 index.html(應用程序的入口點)。

還要檢查 index.html 中的<base href="/"> ,在后端服務器上,我們必須根據路徑渲染 index.html 文件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM