我在执行时遇到这个问题,val.set(key, value)
并TypeError: val.set is not a function
在文件中引发类型错误vendor-es2015.js
。
代码(简体):
import { Storage } from '@ionic/storage';
map = new Map();
this.storage.set('sth', map);
this.storage.get('sth').then((val) => {
val.set(key, value); //TypeError, returns { } insead of a map.
});
有趣的是,这在浏览器中可以正常工作,但是当使用Capacitor将其编译为Android时,会引发此错误。
tsconfig.json
"target": "es2015",
"lib": [
"es2018",
"dom"
]
如文档所述:
存储仅适用于字符串。但是,存储JSON Blob很容易:只需在调用set之前先对对象进行JSON.stringify,然后对JSON.parse从get返回的值进行存储。有关更多详细信息,请参见下面的示例。
可能看起来像这样(在Bergi的帮助下进行了更新):
const mapStr = JSON.stringify(Array.from(map.entries()));
this.storage.set('sth', mapStr);
this.storage.get('sth').then((val) => {
const theMap = new Map(JSON.parse(val));
theMap.set(key, value);
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句