我有两个Map()
:
private dictionaryMap = new Map();
private words = new Map<string, IDictItem>();
填充words
后,我需要将此地图添加到dictionaryMap
中。
那么,如何为dictionaryMap
指定类型?
我尝试过:
private dictionaryMap = new Map<string, Map<string, IDictItem>()>();
但似乎是错误的。
答案 0 :(得分:1)
您需要先像这样初始化它
private dictionaryMap = new Map<string, Map<string, IDictItem>>();
然后用钥匙
将其放入地图中dictionaryMap.set("yourKey", new Map<string, IDictItem>());
答案 1 :(得分:1)
您需要设置值或使用!
声明它们可以不保持开头的初始状态。
interface IDictItem {
definition: string;
}
class Foo {
private words = new Map<string, IDictItem>();
private dictionaryMap: Map<string, Map<string, IDictItem>>;
constructor(){
this.words.set("hello", { definition: "word for greeting" });
this.dictionaryMap = new Map([["key", this.words]])
}
}
关于您的SubjectBehaviour包装。对于您的需求,我缺乏一定的了解,但是如果您需要订阅字典更改的话。那么这样的事情应该有所帮助:
interface IDictItem {
definition: string;
}
class Foo {
private words = new Map<string, IDictItem>([["hello", { definition: "word for greeting" }]]);
private dictionaryMap: Map<string, Map<string, IDictItem>>;
// I was using jsfiddle, so you need to do an actual:
// import { BehaviorSubject } from "rxjs";
private dictionaryMapSubject = new rxjs.BehaviorSubject();
private dictionaryKey = "key"
constructor(){
this.dictionaryMap = new Map([[this.dictionaryKey, this.words]]);
this.dictionaryMapSubject.subscribe(console.log);
}
public publishDic(): void {
const dic = this.dictionaryMap.get(this.dictionaryKey);
this.dictionaryMapSubject.next(dic);
}
}
const foo = new Foo();
foo.publishDic();