【发布时间】:2023-03-15 05:56:01
【问题描述】:
我正在学习角度,我正在尝试将 assets 文件夹中的本地图像加载到 main.component.html 中,并且我已经正确设置了路径,同时我在 alt+单击它打开图像的路径上。但是图片没有加载到浏览器上。
在 chrome 开发者工具的源标签中,没有 assets文件夹包含
我试过这个https://github.com/angular/angular-cli/issues/5268 但没用,请有人帮忙 这是我的 angular.json 文件
{
"$schema": "./node_modules/@angular/cli/lib/config/schema.json",
"version": 1,
"newProjectRoot": "projects",
"projects": {
"game-of-thrones": {
"root": "",
"sourceRoot": "src",
"projectType": "application",
"prefix": "app",
"schematics": {},
"architect": {
"build": {
"builder": "@angular-devkit/build-angular:browser",
"options": {
"outputPath": "dist/game-of-thrones",
"index": "src/index.html",
"main": "src/main.ts",
"polyfills": "src/polyfills.ts",
"tsConfig": "src/tsconfig.app.json",
"assets": [
"src/favicon.ico",
"src/assets"
],
"styles": [
"src/styles.css",
"./node_modules/bootstrap/dist/css/bootstrap.min.css",
"./node_modules/font-awesome/css/font-awesome.css"
],
"scripts": [
"./node_modules/jquery/dist/jquery.min.js",
"./node_modules/bootstrap/dist/js/bootstrap.min.js"
]
},
"configurations": {
"production": {
"fileReplacements": [
{
"replace": "src/environments/environment.ts",
"with": "src/environments/environment.prod.ts"
}
],
"optimization": true,
"outputHashing": "all",
"sourceMap": false,
"extractCss": true,
"namedChunks": false,
"aot": true,
"extractLicenses": true,
"vendorChunk": false,
"buildOptimizer": true
}
}
},
"serve": {
"builder": "@angular-devkit/build-angular:dev-server",
"options": {
"browserTarget": "game-of-thrones:build"
},
"configurations": {
"production": {
"browserTarget": "game-of-thrones:build:production"
}
}
},
"extract-i18n": {
"builder": "@angular-devkit/build-angular:extract-i18n",
"options": {
"browserTarget": "game-of-thrones:build"
}
},
"test": {
"builder": "@angular-devkit/build-angular:karma",
"options": {
"main": "src/test.ts",
"polyfills": "src/polyfills.ts",
"tsConfig": "src/tsconfig.spec.json",
"karmaConfig": "src/karma.conf.js",
"styles": [
"src/styles.css"
],
"scripts": [],
"assets": [
"src/favicon.ico",
"src/assets"
]
}
},
"lint": {
"builder": "@angular-devkit/build-angular:tslint",
"options": {
"tsConfig": [
"src/tsconfig.app.json",
"src/tsconfig.spec.json"
],
"exclude": [
"**/node_modules/**"
]
}
}
}
},
"game-of-thrones-e2e": {
"root": "e2e/",
"projectType": "application",
"architect": {
"e2e": {
"builder": "@angular-devkit/build-angular:protractor",
"options": {
"protractorConfig": "e2e/protractor.conf.js",
"devServerTarget": "game-of-thrones:serve"
}
},
"lint": {
"builder": "@angular-devkit/build-angular:tslint",
"options": {
"tsConfig": "e2e/tsconfig.e2e.json",
"exclude": [
"**/node_modules/**"
]
}
}
}
}
},
"defaultProject": "game-of-thrones"
}
【问题讨论】:
-
你能添加你的 angular.json 吗?
-
你能准确显示哪个 url 给出了 404 错误吗?修改 assets 文件夹后是否重启了 ng serve?
-
@user184994 我添加了angular.json文件看看,
-
@David 这是我的文件夹结构的链接以及我在代码中使用的 img 标签 [link] (imgur.com/a/fpJQgL3),我在修改 assets 文件夹后重新启动了 ng serve,但似乎没有任何效果。
-
您的路径
/../../assets/books.jpg看起来不正确。它应该只是/assets/books.jpg如果不是,则显示 rreal, final 来自 chrome 的网络选项卡的 URL 失败
标签: angular http-status-code-404 assets file-not-found