【发布时间】:2021-12-09 11:07:01
【问题描述】:
我在使用带角度的 json 时遇到了一些问题。
在我的项目中,我生成组件。
然后我删除了 spec.ts。
之后我创建了一个名为 prod.data.json 的文件
{
"products" :
[
{
"sku" : "10",
"name" : "Laptop Blue Background",
"saleTag" : false,
"prodImage" : "img",
"price" : "210",
"discountly" : "200",
"revives" : "3",
"summary" : "is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only",
"availability" : "In Stock",
"categori" : "Business",
"tag" : "Book",
"share" : {
"facebook" : "link",
"twitter" : "link",
"instagram" : "link",
"linkedin" : "link"
},
json代码格式是这样的。
而 content.companents.ts 命名的文件代码是..
import prodData from '/Users/obasekin/angular-crash/src/app/prod.data.json';
interface Product {
sku: Number;
name: String;
saleTag: String;
prodImage: String;
price: Number;
discountly: Number;
revives: Number;
summary: String;
availability: String;
catagori: String;
tag: String;
share: {
facebook: String;
twitter: String;
instagram: String;
linkedin: String;
};
description: {
desSummary: String;
desList: {
lineMono: String;
lineDi: String;
lineTri: String;
lineTetra: String;
linePenta: String;
lineHexa: String;
};
};
}
我导入的格式是字符串或数字。
我的问题出在这段代码中..
@Component({
selector: 'app-content',
templateUrl: './content.component.html',
styleUrls: ['./content.component.css']
})
export class ContentComponent implements OnInit {
constructor() { }
products: Product[] = prodData;
ngOnInit(): void {
}
}
产品在这行有问题
products: Product[] = prodData;
在这里发出 ss..
更新 1
我有 tsconfig.json
"resolveJsonModule": true,
"esModuleInterop": true
其他方式问题ss
问候
【问题讨论】:
-
对不起,我忘记提了。我在 tsconfig.json: "resolveJsonModule": true, "esModuleInterop": true
-
试试
products: Product[] = prodData.products; -
我试了还是一样