问题现象
使用LocalStorage传递function报错:
Error message:@Component 'owning @Component UNKNOWN': Illegal variable value error with decorated variable undefined 'clickSend': failed validation: 'undefined, null, number, boolean, string, or Object but not function, not V2 @ObservedV2 / @Trace class, and makeObserved return value either, attempt to assign value type: 'function', value: 'undefined'!
关键问题参考如下:
function openCommentsInput(title: string, hintMsg: string, clickSend: Function = (content: string) => {}) {
let storage: LocalStorage = new LocalStorage();
storage.setOrCreate('title', title)
storage.setOrCreate('hintMsg', hintMsg)
storage.setOrCreate('clickSend', clickSend) // 程序崩溃,报错
}
背景知识
LocalStorage是页面级的UI状态存储,存储的类型有所限制。
问题定位
查看以下报错日志的关键信息可知,clickSend是非法变量值,支持undefined、null、number、boolean、string、Object等类型,但不支持function函数类型变量。
Illegal variable value error with decorated variable undefined 'clickSend': failed validation: 'undefined, null, number, boolean, string, or Object but not function ...'
分析结论
LocalStorage不支持存储function函数类型变量。
修改建议
使用类class将函数function进行封装为Object对象,LocalStorage支持存储Object对象。
let storage: LocalStorage = new LocalStorage();
function openCommentsInput(title: string, hintMsg: string, clickSend: object) {
storage.setOrCreate('title', title);
storage.setOrCreate('hintMsg', hintMsg);
storage.setOrCreate('clickSend', clickSend);
}
// 使用class对function进行一层封装
class MyFunc {
clickSend: Function = () => {
};
}
// 需要保存至LocalStorage的函数
function clickSend(ctx: string) {
console.info(ctx);
}
@Entry(storage)
@Component
export struct LocalStorageDemo {
@LocalStorageLink('clickSend') myFunc: object = []; // 获取LocalStorage中存储的clickSend对象
build() {
Column({ space: 20 }) {
Button('向storage保存数据')
.onClick(() => {
let myFunc: MyFunc = new MyFunc();
myFunc.clickSend = clickSend; // 将要保存的函数封装在class对象中
openCommentsInput('title', 'hintMsg', myFunc); // 将class对象保存至LocalStorage
});
Button('读取storage的数据')
.onClick(() => {
let tmp = this.myFunc as MyFunc; // 将Object对象转为MyFunc类
tmp.clickSend('读取storage的数据成功'); // 调用MyFunc中的clickSend函数
});
};
}
}
运行截图如下:成功调用LocalStorage中存储的类对象的函数来打印数据。
https://coub.com/view/qk692jnkyy
https://coub.com/view/nqy2r5u04j
https://coub.com/view/rxk7ib6w4i
https://coub.com/view/lfok6c5r0k
https://coub.com/view/cps6sv1832
https://coub.com/view/n4rgmqcpj6
https://coub.com/view/8naf4x8mtj
https://coub.com/view/90hd34u6j5
https://coub.com/view/xxmp511mw1
https://coub.com/view/ga20sx80nf
https://coub.com/view/496cje9x00
https://coub.com/view/e16120c698
https://coub.com/view/qpk44ggqgv
https://coub.com/view/9h64hx23cz
https://coub.com/view/fw89xni3km
https://coub.com/view/fmdlgaxtdh
https://coub.com/view/wbfdz7bao0
https://coub.com/view/jbvjml6xnb
https://coub.com/view/v2gvgqu1pn
https://coub.com/view/lukv9msonl
https://coub.com/view/wy1u881k37
https://coub.com/view/i35bvv7bjj
https://coub.com/view/79bfeowo9y
https://coub.com/view/vfn4r5gd5r
https://coub.com/view/faob3nkydm
https://coub.com/view/t20e00f911
https://coub.com/view/l0n0lv31ss
https://coub.com/view/y1gdff8iup
https://coub.com/view/325iw05lzf
https://coub.com/view/zysxnw2pyg
https://coub.com/view/ip600tbh2t
https://coub.com/view/ik4qxfnchj
https://coub.com/view/x9byx4imcd
https://coub.com/view/j7wpgvy2r9
https://coub.com/view/z9j3mk72kk
https://coub.com/view/u5jraprsbp
https://coub.com/view/18icai1qy2
https://coub.com/view/91t8lsg4vr
https://coub.com/view/zcre25zh9n
https://coub.com/view/4b4f4fhr8u
https://coub.com/view/75reunqnh5
https://coub.com/view/bb1obvuqhi
https://coub.com/view/xxwmrq1it0
https://coub.com/view/zpxfp25pwf
https://coub.com/view/w9qwirmzj7
https://coub.com/view/3he63xj1fl
https://coub.com/view/ij87impqwc
https://coub.com/view/9sllwd1dt7
https://coub.com/view/sap509vv8e
https://coub.com/view/9micgamocw
https://coub.com/view/urk86qp1iu
https://coub.com/view/4tc2me72v9
https://coub.com/view/kq6hr3az82
https://coub.com/view/e7v9yib78m
https://coub.com/view/3me1n3nsrc
https://coub.com/view/ajjah56ium
https://coub.com/view/5fmp56nirq
https://coub.com/view/ouzl95vmsy
https://coub.com/view/5joic8mnfe
https://coub.com/view/wa98vp5r4o
https://coub.com/view/mo88n15ddc
https://coub.com/view/ahpssb3udw
https://coub.com/view/bttevp21v6
https://coub.com/view/wb7er1vpik
https://coub.com/view/u95v6au0cg
https://coub.com/view/w7z0nh2aeb
https://coub.com/view/s2sv3xp1rv
https://coub.com/view/8uw0lqh5t9
https://coub.com/view/3779k4n10i
https://coub.com/view/ks34oh2lf1
https://coub.com/view/p9ypedd2wg
https://coub.com/view/jwiqh1gt58
https://coub.com/view/3mj4f1uew0
https://coub.com/view/zu17pmttav
https://coub.com/view/r2r56ewp66
https://coub.com/view/5ptqyepsog
https://coub.com/view/nfbde2nqvw
https://coub.com/view/8eatx8ni77
https://coub.com/view/4angchwxcu
https://sites.google.com/view/0ngmeh79/home
https://sites.google.com/view/3yrngq53/home
https://sites.google.com/view/3hzydd88/home
https://sites.google.com/view/1ctnmt31/home
https://sites.google.com/view/6sxmmf82/home
https://sites.google.com/view/9pmhfl95/home
https://sites.google.com/view/4jfltz88/home
https://sites.google.com/view/5tysvi63/home
https://sites.google.com/view/0zjxdz60/home
https://sites.google.com/view/0bpoqj02/home
https://sites.google.com/view/8vdldp71/home
https://sites.google.com/view/3uwslk83/home
https://sites.google.com/view/5ltzul21/home
https://sites.google.com/view/8bcvze60/home
https://sites.google.com/view/8qgykm38/home
https://sites.google.com/view/6ajgve57/home
https://sites.google.com/view/5ilxvx17/home
https://sites.google.com/view/4qvlfm93/home
https://sites.google.com/view/4mezwk86/home
https://sites.google.com/view/0atgpp88/home
https://sites.google.com/view/6zfnle48/home
https://sites.google.com/view/6wkkab73/home
https://sites.google.com/view/1uteqg45/home
https://sites.google.com/view/0glrrx12/home
https://sites.google.com/view/4zsavi57/home
https://sites.google.com/view/9oovga45/home
https://sites.google.com/view/3hzgfr61/home
https://sites.google.com/view/9vkbkl41/home
https://sites.google.com/view/6wndzs57/home
https://sites.google.com/view/6uwefj26/home
https://sites.google.com/view/0oipjv09/home
https://sites.google.com/view/4pngoo73/home
https://sites.google.com/view/6fycbf68/home
https://sites.google.com/view/5quyop60/home
https://sites.google.com/view/7oupoi39/home
https://sites.google.com/view/8otdza09/home
https://sites.google.com/view/0sqefg16/home
https://sites.google.com/view/4zrmtg15/home
https://sites.google.com/view/3cgjls08/home
https://sites.google.com/view/6oldtl78/home
https://sites.google.com/view/4ykfdd04/home
https://sites.google.com/view/9wpguc90/home
https://sites.google.com/view/6idlof66/home
https://sites.google.com/view/8omphs74/home
https://sites.google.com/view/6xofvc19/home
https://sites.google.com/view/1difbo72/home
https://sites.google.com/view/4knmhu66/home
https://sites.google.com/view/1ssqkm73/home
https://sites.google.com/view/8zavly68/home
https://sites.google.com/view/9zwrzj64/home
https://sites.google.com/view/7wqfid80/home
https://sites.google.com/view/0mzlvh16/home
https://sites.google.com/view/1hozwq52/home
https://sites.google.com/view/3eikgg48/home
https://sites.google.com/view/3skunx74/home
https://sites.google.com/view/7znrmu32/home
https://sites.google.com/view/9xniil98/home
https://sites.google.com/view/1gmoid98/home
https://sites.google.com/view/6gxmmk76/home
https://sites.google.com/view/6xdnsn40/home
https://sites.google.com/view/6qbvjn86/home
https://sites.google.com/view/9veorf12/home
https://sites.google.com/view/4udhos46/home
https://sites.google.com/view/2ynqpm49/home
https://sites.google.com/view/1pauyq10/home
https://sites.google.com/view/4mmhbb67/home
https://sites.google.com/view/5dimfw85/home
https://sites.google.com/view/7qrywi45/home
https://sites.google.com/view/7xmufr77/home
https://sites.google.com/view/0tzmwq99/home
https://sites.google.com/view/2elngz59/home
https://sites.google.com/view/1mopok42/home
https://sites.google.com/view/5galcb39/home
https://sites.google.com/view/3dplbi09/home
https://sites.google.com/view/4hxzuw21/home
https://sites.google.com/view/3ndtxe40/home
https://sites.google.com/view/1wgxgh74/home
https://sites.google.com/view/0ujdvq38/home
https://sites.google.com/view/3qtkgv13/home
https://sites.google.com/view/5jqmpf47/home
https://sites.google.com/view/6ovyem57/home
https://sites.google.com/view/3jpimw15/home
https://sites.google.com/view/1xlfel73/home
https://sites.google.com/view/8jqklu53/home
https://sites.google.com/view/2vrqgs17/home
https://sites.google.com/view/2cmkar10/home
https://sites.google.com/view/5sktrz87/home
https://sites.google.com/view/9qkbxe89/home
https://sites.google.com/view/2zlonk24/home
https://sites.google.com/view/6jnevv87/home
https://sites.google.com/view/1wrgrh89/home
https://sites.google.com/view/5yjmql75/home
https://sites.google.com/view/1eqrct60/home
https://sites.google.com/view/5bgxgx08/home
https://sites.google.com/view/5ikctu17/home
https://sites.google.com/view/8hqypq15/home
https://sites.google.com/view/9nhoum17/home
https://sites.google.com/view/0pvuts20/home
https://sites.google.com/view/9gqmnb23/home
https://sites.google.com/view/6ifnwy59/home
https://sites.google.com/view/2dacaw12/home
https://sites.google.com/view/9vhqej39/home
https://sites.google.com/view/5tmvjt88/home
https://sites.google.com/view/3mnghf05/home
https://sites.google.com/view/6ttioq69/home
0 个评论
要回复文章请先登录或注册