我定义了一个新的字典类,但不确定如何定义Dictionary
属性。
有一个名为Switches的json文件:
{“ ShowImage”:true,“ ShowText”:false,“ showButton”,true}
import * as switches from '../switches.json';
export class DictionaryClass{
Dictionary?: { [key: string]: boolean };
}
export function getDictionary() {
const dictionaryClass = new DictionaryClass();
dictionaryClass.Dictionary = { [key: string]: boolean };
for (let entry in switches) {
dictionaryClass.Dictionary[entry] = switches[entry];
}
return dictionaryClass;
}
在此示例中,我不确定如何实例化字典,这是我尝试的一种无效的方法:
const dictionaryClass = new DictionaryClass();
dictionaryClass.Dictionary = { [key: string]: boolean };
我也尝试执行以下操作,但它也不起作用
const dictionaryClass = new DictionaryClass();
dictionaryClass.Dictionary = switches;
有人可以向我指出如何正确实例化它吗?而且,不是循环json字典,还是有将整个json字典附加到此dictionary
对象上吗?