[英]How to specify type for Map javascript?
I have two Map()
: 我有两个
Map()
:
private dictionaryMap = new Map();
private words = new Map<string, IDictItem>();
After fill words
I need to add this map into dictionaryMap
. 填充
words
我需要将此地图添加到dictionaryMap
。
So,how to specify type for dictionaryMap
? 那么,如何为
dictionaryMap
指定类型?
I tried: 我试过了:
private dictionaryMap = new Map<string, Map<string, IDictItem>()>();
But seems it is wrong. 但似乎是错误的。
You would need to initialize it first like this 您需要先像这样初始化它
private dictionaryMap = new Map<string, Map<string, IDictItem>>();
and then put it inside the map with a key 然后用钥匙把它放在地图里面
dictionaryMap.set("yourKey", new Map<string, IDictItem>());
You need to set the values or use !
您需要设置值或使用
!
to declare they can remain not initialised at the beginning. 声明它们可以在开始时不进行初始化。
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]])
}
}
About your SubjectBehaviour wrapping. 关于您的SubjectBehaviour包装。 I lack some context as to what your needs are but if what you need is to subscribe to dictionary changes.
对于您的需求,我缺乏一定的了解,但是如果您需要订阅字典更改的话。 then something like this should help:
那么这样的事情应该有所帮助:
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();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.