I am looking to display data in an HTML file that is sourced from a local JSON file.
The structure of my JSON file is as follows:
{
"ACCESSIBILITY_EXPANDED": "Expanded",
"ACCESSIBILITY_BACK": "Back",
"ACCESSIBILITY_COLLAPSED": "Collapsed",
"ACCESSIBILITY_PHONE_NUMBER": "Call us at",
"ACCESSIBILITY_EMAIL": "Email us at",
"ACCESSIBILITY_ADDRESS": "Visit us at",
"ACCESSIBILITY_FAX": "Send us a FAX at",
}
To achieve this, I made modifications to my tsconfig.json file:
{
"compileOnSave": false,
"compilerOptions": {
"baseUrl": "./",
"outDir": "./dist/out-tsc",
"sourceMap": true,
"declaration": false,
"module": "es2015",
"moduleResolution": "node",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"target": "es5",
"typeRoots": [
"node_modules/@types",
"src/typings.d.ts"
],
"lib": [
"es2018",
"dom"
],
"allowSyntheticDefaultImports": true,
"resolveJsonModule": true,
"esModuleInterop": true,
}
}
I also created a typings.d.ts file within my src folder with the following content:
declare module "*.json" {
const value: any;
export default value;
}
In my component's .ts file, I added the following line:
import en from '../../assets/i18n/en.json';
However, when I tried to use the data in my HTML file like this
<span class="bolded"> {{ en.ACCESSIBILITY_BACK }}</span>
I encountered an error stating "Cannot read property ACCESSIBILITY_BACK of undefined" even though the object is logged properly when I console.log(en) in ngOnInit(). Any suggestions on how to resolve this issue? Thank you.