我不确定在这里做错了什么,因为我在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"
}
providers: [{provide: LocationStrategy, useClass: HashLocationStrategy}]
解决了问题,是的,虽然URL很丑陋,但我想我可以通过.htaccess配置来解决。感谢您抽出时间进行解释,否则我永远也不会想到。 - James