[英]Error TS2339: Property 'default' does not exist on type 'Special[]'
I am writing an app in Angular.我正在 Angular 中编写一个应用程序。 I am using json file, by getting data from it in the ts file:
我正在使用 json 文件,通过从 ts 文件中获取数据:
import * as data from '../../special.json';
export class SpecialComponent {
specials = (data as any).default;
}
And everything is fine.一切都很好。 But when I want to add type instead of any:
但是当我想添加类型而不是任何时:
import * as data from '../../special.json';
export class SpecialComponent {
specials = (data as Special[]).default;
}
I get an error:我收到一个错误:
error TS2339: Property 'default' does not exist on type 'Special[]'.
This is my interface on service:这是我的服务界面:
export interface Special {
category: string;
name: string;
description: string;
type?: string;
imageUrl?: string;
}
This is my json file:这是我的 json 文件:
[
{
"category": "xxx",
"name": "xxx",
"description": "xxx",
"type": "xxx",
"imageUrl": [
"assets/img/img.jpg",
"assets/img/img1.jpg"
]
}
]
The error is correct, because there is no default
on Special[]
.错误是正确的,因为
Special[]
上没有default
。
One easy way to do what you are trying to do is:做你想做的事情的一种简单方法是:
Put resolveJsonModule
and esModuleInterop
in tsconfig.json
file of project.将
resolveJsonModule
和esModuleInterop
放入项目的tsconfig.json
文件中。
"compilerOptions": {
"resolveJsonModule": true,
"esModuleInterop": true,
....
}
and then import:然后导入:
import {default as data} from '../../special.json';
export class SpecialComponent {
specials = data as Special[];
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.