【问题标题】:How to map the interface for json structure in Angular6如何在Angular6中映射json结构的接口
【发布时间】:2019-03-09 12:21:34
【问题描述】:

组件.ts

import * as AssetId from "../../../assetid.json";

export class AssetID {
    abc: number;
    xxx: number;
}

export class Sample {
    public assetId: AssetID = AssetId; // Line no : 9
}

assetid.json

{
    "abc": 3,
    "xxx": 4
}

在第 9 行,它会产生类似

的错误

TS2322:类型 'typeof ".json"' 不可分配给类型 'AssetID'。 'typeof ".json"' 类型中缺少属性 'abc'

我想将此 json 格式映射为 void 'any' 打字稿代码中的类型。

【问题讨论】:

    标签: angular


    【解决方案1】:

    你应该这样使用

    interface IAssetid {
        abc: number;
        xxx: number;
    }
    

    其次,您应该从 ts 文件而不是 json 导入 const

    export const assetid = {
        'abc': 3,
        'xxx': 4
    };
    

    欲了解更多信息,请阅读此https://stackoverflow.com/a/46991300/3326275

    【讨论】:

      猜你喜欢
      • 2015-06-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多