收藏
回答

鸿蒙(1.0.3.42版本)微信小程序,不支持TextEncoder? ios和安卓都支持

在worker进程和主进程里使用TextEncoder都报错, ios和安卓都支持,期望官方可以适配下


回答关注问题邀请回答
收藏

1 个回答

  • 虎子
    虎子
    2025-02-14

    deepseek给了一个兼容写法,目前在我的业务中,可用

    function encodeString(str) {

      if (typeof TextEncoder !== 'undefined') {

        const textEncoder = new TextEncoder();

        return textEncoder.encode(str);

      } else {

        // 兼容低版本微信小程序

        const codePoints = [];

        for (let i = 0; i < str.length; i++) {

          const codePoint = str.charCodeAt(i);

          codePoints.push(codePoint);

        }

        return new Uint8Array(codePoints);

      }

    }


    const uint8Array = encodeString('Hello');

    console.log(uint8Array); // 输出: Uint8Array [72, 101, 108, 108, 111]


    2025-02-14
    有用
    回复
登录 后发表内容