I have this error:
Type {...} is missing the following properties from type 'any[]': length,pop,push,concat, and 28 more
I have this error:
Type {...} is missing the following properties from type 'any[]': length,pop,push,concat, and 28 more
I'm assigning album variable to the albumData array. Here is the code:
import { Component, OnInit } from '@angular/core';
import albumData from '../data/SearchResultsAlbum.json';
@Component({
selector: 'app-album-ponent',
templateUrl: './album-ponent.ponent.html',
styleUrls: ['./album-ponent.ponent.css']
})
export class AlbumComponentComponent implements OnInit {
album:Array<any>=[];
constructor() { }
ngOnInit(): void {
this.album=albumData;
}
}
What am I doing wrong? Thank you in advanvce!
Share Improve this question asked Mar 2, 2022 at 16:46 MaxMax 1191 gold badge1 silver badge8 bronze badges 1- Either your json is an object, so it won't have those properties that are array properties, or your json data needs to be parsed first anyway in order to use it as array. What do you get when you console.log the albumData? – Misha Mashina Commented Mar 2, 2022 at 16:55
2 Answers
Reset to default 9 album:any=[];
use this code instead of the code you have added
By default, Angular doesn't read the JSON file in the application. So we need to do some extra stuff for that. So we will create a file named 'json-typings.d.ts' inside the app folder of the project.
declare module "*.json" {
const value: any;
export default value;
}
source: https://jsonworld./demo/how-to-read-local-json-file-in-angular
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1740932903a4277357.html
评论列表(0条)