Angular 路由在 运行 ng 部署后不工作

Angular routing not working after running ng build at deployment

不确定我在这里做错了什么,我是 Angular 的新手,并且在路由方面有些摸索。

我是 运行ning Angular 12.2.0 并在本地主机上设置了路由。我现在只是在两个页面之间导航,看看它是如何工作的,它在本地工作正常。

例如,在 localhost 上,我可以从根目录 http://localhost:4200/ 浏览到 http://localhost:4200/locations,效果很好。

 const routes: Routes = [
  {path: '', component: HomeComponent},
  {path: 'locations', component: LocationsComponent}
];

当我运行ng build构建项目时,它运行很好,我可以打开index.html很好

我不能做的就是不再在路线之间导航

当我点击位置 link 时在 dist/angular-app/index.html 启动构建,它想要 link 到 dist/angular-app/index.html/locations 即使我更改 [=42] 也是空白=] 到 dist/angular-app/locations 这也是空白

有人指出我为什么这样做的正确方向吗?

非常感谢

附上我的 angular.json

{
  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "version": 1,
  "newProjectRoot": "projects",
  "projects": {
    "angular-app": {
      "projectType": "application",
      "schematics": {
        "@schematics/angular:component": {
          "style": "scss"
        },
        "@schematics/angular:application": {
          "strict": true
        }
      },
      "root": "",
      "sourceRoot": "src",
      "prefix": "app",
      "architect": {
        "build": {
          "builder": "@angular-devkit/build-angular:browser",
          "options": {
            "outputPath": "dist/angular-app",
            "index": "src/index.html",
            "main": "src/main.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "tsconfig.app.json",
            "inlineStyleLanguage": "scss",
            "assets": [
              "src/favicon.ico",
              "src/assets"
            ],
            "styles": [
              "src/styles.scss"
            ],
            "scripts": []
          },
          "configurations": {
            "production": {
              "budgets": [
                {
                  "type": "initial",
                  "maximumWarning": "500kb",
                  "maximumError": "1mb"
                },
                {
                  "type": "anyComponentStyle",
                  "maximumWarning": "2kb",
                  "maximumError": "4kb"
                }
              ],
              "fileReplacements": [
                {
                  "replace": "src/environments/environment.ts",
                  "with": "src/environments/environment.prod.ts"
                }
              ],
              "outputHashing": "all"
            },
            "development": {
              "buildOptimizer": false,
              "optimization": false,
              "vendorChunk": true,
              "extractLicenses": false,
              "sourceMap": true,
              "namedChunks": true
            }
          },
          "defaultConfiguration": "production"
        },
        "serve": {
          "builder": "@angular-devkit/build-angular:dev-server",
          "configurations": {
            "production": {
              "browserTarget": "angular-app:build:production"
            },
            "development": {
              "browserTarget": "angular-app:build:development"
            }
          },
          "defaultConfiguration": "development"
        },
        "extract-i18n": {
          "builder": "@angular-devkit/build-angular:extract-i18n",
          "options": {
            "browserTarget": "angular-app:build"
          }
        },
        "test": {
          "builder": "@angular-devkit/build-angular:karma",
          "options": {
            "main": "src/test.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "tsconfig.spec.json",
            "karmaConfig": "karma.conf.js",
            "inlineStyleLanguage": "scss",
            "assets": [
              "src/favicon.ico",
              "src/assets"
            ],
            "styles": [
              "src/styles.scss"
            ],
            "scripts": []
          }
        }
      }
    }
  },
  "defaultProject": "angular-app"
}

很可能是因为您的网络服务器实际上正在尝试查找与 URL 对应的页面。但是,由于 Angular 应用程序是 SPA,元素会根据浏览器中的 url 动态注入页面,因此,在请求路由时,服务器会出现 returns 404 HTTP 错误。 这可以通过使用 angular 中的 HashLocationStrategy 来解决,它会在您的 URL 后附加一个 #,这样就可以在部署应用程序时进行导航。

将此添加到供应商部分的 app.module.ts

providers: [{provide: LocationStrategy, useClass: HashLocationStrategy}]

导入:

import { HashLocationStrategy, LocationStrategy } from '@angular/common';

编辑: 或者,如果您通过 Nginx 或 Apache 服务器为应用程序提供服务,则可以在配置文件中添加配置条目以将所有没有匹配文件的请求重定向到 /index.html。 更多相关内容,您可以查看 https://angular.io/guide/deployment#server-configuration