gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 09:28:49 24 4
gpt4 key购买 nike

不确定我在这里做错了什么,我对 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 启动构建,它想要链接到 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"
}

最佳答案

很可能是因为您的网络服务器实际上正在尝试查找与 URL 对应的页面。但是,由于 Angular 应用程序是一个 SPA,元素会根据浏览器中的 url 动态注入(inject)到页面中,因此,在请求路由时,服务器会返回 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

关于在部署时运行 ng build 后 Angular 路由不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71094093/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com