Angular 6 资产文件夹未在浏览器上呈现
Angular 6 assets folder is not getting rendered on browser
我正在学习 angular,我正在尝试将 assets 文件夹中的本地图像加载到 main.component.html 并且我已正确设置路径,同时我按 alt+单击它打开图像的路径。但是图片没有在浏览器上加载。
我得到的错误
在 chrome 开发人员工具的源选项卡中,没有 assets 文件夹包含
这是我的 angular cli 版本
我试过了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"
}
通常你需要在你的 index.html 中添加你的 base href 前缀 :
base href="/game-of-thrones/"
为了在正确的文件夹中查找资产。
我有同样的问题
GET http://localhost:4200/assets/img/img.png 404 (Not Found)
但我通过删除项目路径中的所有特殊字符('('、'@'、'$' 等)修复了它。
例如,如果您有一个像
这样的项目路径
C:\angular-projects(1)\angularapp
然后改成
C:\angularprojects1\angularapp
通过删除应用程序文件夹位置的所有特殊字符、符号找到了解决上述问题的方法,看看这个Github issue。
比如你的项目在E盘,
更改此位置
E:\Data\project - 1\Angular(learning)\app
到
E:\Data\project1\Angularlearning\app
然后您将能够在 chrome 开发人员工具中的源选项卡下看到加载到浏览器的资产文件夹。
我正在学习 angular,我正在尝试将 assets 文件夹中的本地图像加载到 main.component.html 并且我已正确设置路径,同时我按 alt+单击它打开图像的路径。但是图片没有在浏览器上加载。
我得到的错误
在 chrome 开发人员工具的源选项卡中,没有 assets 文件夹包含
这是我的 angular cli 版本
我试过了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"
}
通常你需要在你的 index.html 中添加你的 base href 前缀 :
base href="/game-of-thrones/"
为了在正确的文件夹中查找资产。
我有同样的问题
GET http://localhost:4200/assets/img/img.png 404 (Not Found)
但我通过删除项目路径中的所有特殊字符('('、'@'、'$' 等)修复了它。 例如,如果您有一个像
这样的项目路径C:\angular-projects(1)\angularapp
然后改成
C:\angularprojects1\angularapp
通过删除应用程序文件夹位置的所有特殊字符、符号找到了解决上述问题的方法,看看这个Github issue。
比如你的项目在E盘,
更改此位置
E:\Data\project - 1\Angular(learning)\app
到
E:\Data\project1\Angularlearning\app
然后您将能够在 chrome 开发人员工具中的源选项卡下看到加载到浏览器的资产文件夹。