部署后运行ng build,Angular路由不起作用

4

我不确定在这里做错了什么,因为我在Angular方面还比较新手,并且在路由方面有些摸不着头脑。

我正在使用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。
但是我不能再路由之间导航了。
当我点击位置链接时,它想要链接到dist/angular-app/index.html/locations,但这是空的,即使我将URL更改为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"
}
3个回答

14
很可能是因为你的 Web 服务器实际上正在尝试查找与 URL 相对应的页面。但是,由于 Angular 应用程序是单页应用程序,元素是根据浏览器中的 URL 动态注入到页面中的。因此,在请求路由时,服务器返回 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


谢谢Akmal,添加providers: [{provide: LocationStrategy, useClass: HashLocationStrategy}]解决了问题,是的,虽然URL很丑陋,但我想我可以通过.htaccess配置来解决。感谢您抽出时间进行解释,否则我永远也不会想到。 - James
有没有其他方法可以不添加哈希进行操作。我不想使用哈希。 - Sandeep Bhaskar
1
@SandeepBhaskar 是的,你可以按照这个链接 https://angular.io/guide/deployment#server-configuration 进行操作。 - Krishantha Dinesh

2
如果使用 Nginx 进行托管,请更改。
location / {
        try_files $uri $uri/ =404;
}

location / {
        try_files $uri $uri/ /index.html;
}

这不需要 Akmal 的答案中提到的 HashLocationStrategy。


0
如果您在Web应用程序中定义了错误页面,请确保将该路径放在上面,否则它将始终触发错误页面。
文件名:app-routing.module.ts

enter image description here


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接