angular - 在 Angular 中创建一个可观察的 Dictionary/HashMap(或 KeyValue 对)?
问题描述
我想创建一个可观察的 KeyPairValue(或 Map,但我不知道该怎么做)。
这是我的声明:
private readonly subjectTokensKeyValue = new Subject<{ [key: string]: [value: string] }>();
public readonly tokensKeyValue$ = this.subjectTokensKeyValue.asObservable();
现在我有一个方法可以将值传递给我的 observable
public setTokens(tokens: { [key: string]: [value: string] }) {
this.subjectTokensKeyValue.next(tokens);
}
现在我需要初始化theTokens
数组(但是,我不知道该怎么做!!!)
const theTokens = [];///HERE!!!
const token = {
key: "theKey",
value: "theValue",
};
theTokens.push(token);
后来我尝试传递值
this.setTokens(theTokens); /// HERE I Have a Problem
问题信息是
'{ key: any; 类型的参数 值:任何;}[]' 不能分配给 '{ [key: string]: [value: string]; 类型的参数 }'。
类型 '{ key: any; 中缺少类型 'string' 的索引签名;值:任何;}[]'.ts(2345)
我必须如何初始化theTokens
?
解决方案
推荐阅读
- cmake - 避免包含的 cmake 文件中的绝对路径
- mysql - 错误:ModuleNotFoundError:没有名为“MySQLdb”的模块
- python - 在 Python 中使用 Beautiful Soup 抓取多个具有相同名称的 div 类
- hive - 如何将包含选项卡和其他任意字符的数据从 Hive 导出到磁盘?
- debian - 尝试在 Debian 上安装 ppa 包
- reactjs - 带有打字稿的反应传单 GeoJSON
- python - 无法将字符串转换为浮点数:.prn 中的“”
- r - 如何更改列名并制作数据集中列的数据框
- c++ - 带有 SPI_GETMOUSE 的 SystemParametersInfoA 返回 0
- python - sys.path 不会自动包含当前工作目录