我有这个错误:
Type {...} is missing the following properties from type 'any[]': length,pop,push,concat, and 28 more
我正在将相册变量分配给albumData数组。这是代码:
import { Component, OnInit } from '@angular/core';
import albumData from '../data/SearchResultsAlbum.json';
@Component({
selector: 'app-album-component',
templateUrl: './album-component.component.html',
styleUrls: ['./album-component.component.css']
})
export class AlbumComponentComponent implements OnInit {
album:Array<any>=[];
constructor() { }
ngOnInit(): void {
this.album=albumData;
}
}
我做错了什么?提前谢谢!
album:any=[];
使用此代码而不是您添加的代码
默认情况下,Angular不会读取应用程序中的JSON文件。所以我们需要为此做一些额外的事情。因此,我们将在项目的应用程序文件夹中创建一个名为"json-typetings.d.ts"的文件。
declare module "*.json" {
const value: any;
export default value;
}
来源:https://jsonworld.com/demo/how-to-read-local-json-file-in-angular