[英]Can't read data from JSON file in local folder in Angular 7
我有下面的代碼,它工作正常。
searchData() {
const url: any = 'https://jsonplaceholder.typicode.com/photos?albumId=1';
this.http.get(url).subscribe((res) => {
this.data = res;
console.log('Response Returned');
},
err => {
console.log('Error Response');
});
Output:響應返回預期:響應返回
現在,我想從保存在 assets 文件夾中的本地 JSON 文件中獲取數據,而不是 URL,所以我更改了路徑並給出了 Z0ECD11C1D7A287401D148A23BBD7 文件的路徑作為回應,我得到錯誤響應。 有人可以幫我弄清楚如何從本地 JSON 文件中讀取嗎?
本地 JSON 文件的代碼
searchData() {
const ucv_data: any = 'src/assets/json/ucv_json.json';
this.http.get(ucv_data).subscribe((res) => {
this.data = res;
console.log('Response Returned');
},
err => {
console.log('Error Response');
}
);
}
Output:預期錯誤響應:返回響應
Angular.json文件
{
"$schema": "./node_modules/@angular/cli/lib/config/schema.json",
"version": 1,
"newProjectRoot": "projects",
"projects": {
"test-ng7": {
"root": "",
"sourceRoot": "src",
"projectType": "application",
"architect": {
"build": {
"builder": "@angular-devkit/build-angular:browser",
"options": {
"outputPath": "dist",
"index": "src/index.html",
"main": "src/main.ts",
"tsConfig": "src/tsconfig.app.json",
"polyfills": "src/polyfills.ts",
"assets": [
"src/assets/json",
"src/assets/images",
"src/favicon.ico"
],
"styles": [
"./node_modules/@angular/material/prebuilt-themes/deeppurple-amber.css",
"./node_modules/bootstrap/dist/css/bootstrap.min.css",
"./node_modules/font-awesome/css/font-awesome.css",
"./node_modules/font-awesome/css/font-awesome.min.css",
"src/styles/app.scss","src/assets/css/material.scss",
"src/assets/css/styles.scss"
],
"scripts": [
"node_modules/chart.js/dist/Chart.js",
"./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,
"budgets": [
{
"type": "initial",
"maximumWarning": "2mb",
"maximumError": "5mb"
}
]
}
}
},
"serve": {
"builder": "@angular-devkit/build-angular:dev-server",
"options": {
"browserTarget": "test-ng7:build"
},
"configurations": {
"production": {
"browserTarget": "test-ng7:build:production"
}
}
},
"extract-i18n": {
"builder": "@angular-devkit/build-angular:extract-i18n",
"options": {
"browserTarget": "test-ng7: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",
"scripts": [
"node_modules/chart.js/dist/Chart.js"
],
"styles": [
"./node_modules/@angular/material/prebuilt-themes/deeppurple-amber.css",
"node_modules/font-awesome/css/font-awesome.css",
"src/styles/app.scss"
],
"assets": [
"src/assets",
"src/assets/images",
"src/favicon.ico"
]
}
},
"lint": {
"builder": "@angular-devkit/build-angular:tslint",
"options": {
"tsConfig": [
"src/tsconfig.app.json",
"src/tsconfig.spec.json"
],
"exclude": [
"**/node_modules/**"
]
}
}
}
},
"test-ng7-e2e": {
"root": "e2e/",
"projectType": "application",
"prefix": "",
"architect": {
"e2e": {
"builder": "@angular-devkit/build-angular:protractor",
"options": {
"protractorConfig": "e2e/protractor.conf.js",
"devServerTarget": "test-ng7:serve"
},
"configurations": {
"production": {
"devServerTarget": "test-ng7:serve:production"
}
}
},
"lint": {
"builder": "@angular-devkit/build-angular:tslint",
"options": {
"tsConfig": "e2e/tsconfig.e2e.json",
"exclude": [
"**/node_modules/**"
]
}
}
}
}
},
"defaultProject": "test-ng7",
"schematics": {
"@schematics/angular:component": {
"prefix": "app",
"styleext": "scss"
},
"@schematics/angular:directive": {
"prefix": "app"
}
}
}
該路徑是相對於src
文件夾的。 嘗試
const ucv_data: any = 'assets/json/ucv_json.json';
根據 OP 的評論, assets
屬性定義是
"assets": [
"src/assets/json",
"src/assets/images",
"src/favicon.ico"
]
所以應該調用位置/json/ucv_json.json
。
使用 Angular 7+ 我們可以導入 JSON 之類的模塊。
在 tsconfig 文件中放置:
{ "compilerOptions": { "resolveJsonModule": true, "esModuleInterop": true } }
然后你可以像這樣導入它。
import ucv_data from "assets/json/ucv_json.json"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.