Jam*_*mes 5 routes build angular ng-build
不知道我在这里做错了什么,我对 Angular 相当陌生,并且在路由方面有些摸索。
我正在运行 Angular 12.2.0 并在本地主机上设置了路由。我现在只是在两个页面之间导航,看看它是如何工作的,它在本地工作得很好。
例如,在 localhost 上,我可以从根 http://localhost:4200/ 浏览到 http://localhost:4200/locations,效果很好。
const routes: Routes = [
{path: '', component: HomeComponent},
{path: 'locations', component: LocationsComponent}
];
Run Code Online (Sandbox Code Playgroud)
当我运行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"
}
Run Code Online (Sandbox Code Playgroud)
Akm*_*mal 17
最有可能的是因为您的网络服务器实际上正在尝试查找与该 URL 相对应的页面。然而,由于 Angular 应用程序是 SPA,元素会根据浏览器中的 url 动态注入到页面中,因此,在请求路由时,服务器会返回 404 HTTP 错误。这可以通过使用 Angular 中的 HashLocationStrategy 来解决,它将 # 附加到您的 URL 中,以便在部署应用程序时进行导航。
将其添加到提供程序部分的app.module.ts中。
providers: [{provide: LocationStrategy, useClass: HashLocationStrategy}]
Run Code Online (Sandbox Code Playgroud)
进口:
import { HashLocationStrategy, LocationStrategy } from '@angular/common';
Run Code Online (Sandbox Code Playgroud)
编辑:或者,如果您通过 Nginx 或 Apache 服务器为应用程序提供服务,则可以在配置文件中添加配置条目,以将没有匹配文件的所有请求重定向到 /index.html。有关更多信息,您可以查看https://angular.io/guide/deployment#server-configuration
| 归档时间: |
|
| 查看次数: |
15550 次 |
| 最近记录: |