【发布时间】:2020-05-25 19:53:48
【问题描述】:
我在 Agular 9 的一个根模块中有一个多组件应用程序。
问题是当我通过 ng build --prod 部署它并将其从 dist 文件夹复制到我的本地 apache XAMPP 服务器时,除了图像之外,一切正常。
错误信息是:
加载资源失败:服务器响应状态为 404(未找到) 我已经尝试更改图像的路径,但它没有工作。
我做错了什么?
在 index.html 中我设置了<base href="/travelAgency/">
这是我添加的 angular.json
"baseHref" : "/travelAgency/", "deployUrl": "/travelAgency/"
"$schema": "./node_modules/@angular/cli/lib/config/schema.json",
"version": 1,
"newProjectRoot": "projects",
"projects": {
"travelAgency": {
"projectType": "application",
"schematics": {},
"root": "",
"sourceRoot": "src",
"prefix": "app",
"architect": {
"build": {
"builder": "@angular-devkit/build-angular:browser",
"options": {
"baseHref" : "/travelAgency/",
"deployUrl": "/travelAgency/",
"outputPath": "dist/travelAgency",
"index": "src/index.html",
"main": "src/main.ts",
"polyfills": "src/polyfills.ts",
"tsConfig": "tsconfig.app.json",
"aot": true,
"assets": [
"src/favicon.ico",
"src/assets"
],
"styles": [
"node_modules/bootstrap/dist/css/bootstrap.min.css",
"src/styles.css"
],
"scripts": []
},
"configurations": {
"production": {
"fileReplacements": [
{
"replace": "src/environments/environment.ts",
"with": "src/environments/environment.prod.ts"
}
],
"optimization": true,
"outputHashing": "all",
"sourceMap": false,
"extractCss": true,
"namedChunks": false,
"extractLicenses": true,
"vendorChunk": false,
"buildOptimizer": true,
"budgets": [
{
"type": "initial",
"maximumWarning": "2mb",
"maximumError": "5mb"
},
{
"type": "anyComponentStyle",
"maximumWarning": "6kb",
"maximumError": "10kb"
}
]
}
}
},
"serve": {
"builder": "@angular-devkit/build-angular:dev-server",
"options": {
"browserTarget": "travelAgency:build"
},
"configurations": {
"production": {
"browserTarget": "travelAgency:build:production"
}
}
},
"extract-i18n": {
"builder": "@angular-devkit/build-angular:extract-i18n",
"options": {
"browserTarget": "travelAgency: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",
"assets": [
"src/favicon.ico",
"src/assets"
],
"styles": [
"src/styles.css"
],
"scripts": []
}
},
"lint": {
"builder": "@angular-devkit/build-angular:tslint",
"options": {
"tsConfig": [
"tsconfig.app.json",
"tsconfig.spec.json",
"e2e/tsconfig.json"
],
"exclude": [
"**/node_modules/**"
]
}
},
"e2e": {
"builder": "@angular-devkit/build-angular:protractor",
"options": {
"protractorConfig": "e2e/protractor.conf.js",
"devServerTarget": "travelAgency:serve"
},
"configurations": {
"production": {
"devServerTarget": "travelAgency:serve:production"
}
}
}
}
}},
"defaultProject": "travelAgency"
}
【问题讨论】:
-
也许添加图像的 url 在 prod mod 中的样子(即 url 给出 404)和你的 apache 配置
-
感谢您的建议!!!当我在浏览器中检查部署的元素时,它是 url(/assets/hero-image.jpg) 就像我在源代码中一样。然后在 chrome 检查器中,我删除了第一个 / 他立即找到了图像。问题是当我在 prod 模式下删除第一个斜杠时,他给了我一个错误。无法解析 url("assets/hero-image.jpg")。
标签: angular deployment angular9