代码如下
@Sendable
export default class InsertMsgDbModel {
public tableName: string = '';
public dbName: string = '';
public userId: string = GlobalConf.channel;
public sendableContext: sendableContextManager.SendableContext;
constructor(sendableContext: sendableContextManager.SendableContext) {
this.sendableContext = sendableContext;
}
}
let sendableContext: sendableContextManager.SendableContext = sendableContextManager.convertFromContext(this.context);
const insertDbModel = new InsertMsgDbModel(sendableContext);
private workerStage1: worker.ThreadWorker = new worker.ThreadWorker('ZhBaseLib/ets/workers/ZHBaseWorker.ets');
this.workerStage1.postMessage(insertDbModel);
删除sendableContext 后可以正常传递,加上后提示序列化失败。按官方文档应该是支持的。
使用sendableContext需要配合使用postMessageWithSharedSendable方法,这边看您使用的是postMessage,可尝试替换为postMessageWithSharedSendablesendableContextManager.convertFromContext的示例中使用的也是postMessageWithSharedSendable,请替换使用
参考链接:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-worker-V5\#postmessagewithsharedsendable12
https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-app-ability-sendablecontextmanager-V5\#sendablecontextmanagerconvertfromcontext