我有两个Map()
:
private dictionaryMap = new Map();
private words = new Map<string, IDictItem>();
填充words
后,我需要将此 map 添加到dictionaryMap
中。
那么,如何为dictionaryMap
指定类型?
我尝试过:
private dictionaryMap = new Map<string, Map<string, IDictItem>()>();
但似乎这是错误的。
最佳答案
您需要设置这些值或使用!
来声明它们可以在开始时保持未初始化状态。
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();
关于javascript - 如何指定Map javascript的类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56075292/