vue对于低版本浏览器兼容问题

准备

由于采用了vite3而不是vue-cli,所以以前的很多兼容方式都不能做。接下来就看一下vite是怎么做到低版本兼容的问题。

工具库

@vitejs/plugin-legacyds
官方唯一指定的兼容工具库,使用方式官网都有了

进阶使用

问题例子

虽然有些确实是兼容了低版本,但是,有些工具库利用了些新的特性,页面还是报错。

比如下面这个在低版本手机的报错,例子是我们这个框架中,去掉modernPolyfills:['es.array.flat-map','es.object.values'],的兼容性:

[Vue warn]: Unhandled error during execution of watcher callback 
  at <VanConfig> 
  at <App>
[Vue warn]: Unhandled error during execution of setup function 
  at <VanConfig> 
  at <App>
Uncaught TypeError: Object.values(...).flatMap is not a function\n\t/viteTest/assets/index.44986ed0.js:46:12228\nTypeError: Object.values(...).flatMap is not a function
    at getSSRHandler (https://test.dongguantong.com.cn/viteTest/assets/index.44986ed0.js:46:12228)
    at A (https://test.dongguantong.com.cn/viteTest/assets/index.44986ed0.js:46:12422)
    at Object.onChanged (https://test.dongguantong.com.cn/viteTest/assets/index.44986ed0.js:46:13520)
    at x (https://test.dongguantong.com.cn/viteTest/assets/index.44986ed0.js:46:12476)
    at callWithErrorHandling (https://test.dongguantong.com.cn/viteTest/assets/index.44986ed0.js:4:1576)
    at callWithAsyncErrorHandling (https://test.dongguantong.com.cn/viteTest/assets/index.44986ed0.js:4:1698)
    at I (https://test.dongguantong.com.cn/viteTest/assets/index.44986ed0.js:4:17067)
    at doWatch (https://test.dongguantong.com.cn/viteTest/assets/index.44986ed0.js:4:17371)
    at watch (https://test.dongguantong.com.cn/viteTest/assets/index.44986ed0.js:4:15741)
    at useColorMode (https://test.dongguantong.com.cn/viteTest/assets/index.44986ed0.js:46:12503)
[Vue warn]: Unhandled error during execution of watcher callback 
  at <VanConfig> 
  at <App>
[Vue warn]: Unhandled error during execution of setup function 
  at <VanConfig> 
  at <App>
[Vue warn]: Unhandled error during execution of scheduler flush. This is likely a Vue internals bug. Please open an issue at https://new-issue.vuejs.org/?repo=vuejs/core 
  at <VanConfig> 
  at <App>
[Vue Router warn]: uncaught error during route navigation:
{}
Uncaught (in promise)  {"name": "TypeError", "message": "Object.values(...).flatMap is not a function", "stack": "TypeError: Object.values(...).flatMap is not a function\n    at getSSRHandler (https://test.dongguantong.com.cn/viteTest/assets/index.44986ed0.js:46:12228)\n    at A (https://test.dongguantong.com.cn/viteTest/assets/index.44986ed0.js:46:12422)\n    at Object.onChanged (https://test.dongguantong.com.cn/viteTest/assets/index.44986ed0.js:46:13520)\n    at x (https://test.dongguantong.com.cn/viteTest/assets/index.44986ed0.js:46:12476)\n    at callWithErrorHandling (https://test.dongguantong.com.cn/viteTest/assets/index.44986ed0.js:4:1576)\n    at callWithAsyncErrorHandling (https://test.dongguantong.com.cn/viteTest/assets/index.44986ed0.js:4:1698)\n    at I (https://test.dongguantong.com.cn/viteTest/assets/index.44986ed0.js:4:17067)\n    at doWatch (https://test.dongguantong.com.cn/viteTest/assets/index.44986ed0.js:4:17371)\n    at watch (https://test.dongguantong.com.cn/viteTest/assets/index.44986ed0.js:4:15741)\n    at useColorMode (https://test.dongguantong.com.cn/viteTest/assets/index.44986ed0.js:46:12503)"}
Unhandled promise rejection {}

解决思路

语法不支持

Object.values(...).flatMap is not a function

我们就可以从中推断出,肯定是某个库,用了高级语法,然后低版本没兼容。因为在es6以上flatMap、Object.values都是支持的,但是我们目前不知道哪个有。

具体哪个使用了哪个库不支持

然后又根据

[Vue warn]: Unhandled error during execution of watcher callback 
  at <VanConfig> 
  at <App>

可以确认,就是我们自己些的VanConfig组件有某个库不被支持了

然后我们点进去,这个库其实就只是应用到了vueUse中的useDark。

我们查历史可以得知,在安卓6左右,是没有暗黑模式这个概念的。我们把这个useDark组件去掉,再打包。重新打开,我们就确实能够在低版本手机中看到了

兼容语法

但是把某个库或者某个功能去掉,肯定是下下策,最好还是能够语法兼容。

查阅文档,其中有2个专门将高级语法转换的,是polyfills和modernPolyfills。根据文档,我们可以得知,手动将高级语法转换的方式是这样

import legacy from '@vitejs/plugin-legacy'

export default {
  plugins: [
    legacy({
      polyfills: ['es.promise.finally', 'es/map', 'es/set'],
      modernPolyfills: ['es.promise.finally']
    })
  ]
}

但文档写得不是很好,没有具体说明polyfills和modernPolyfills的关系,我还是建议2个都写得一样。
具体有哪些可以设置的值,就是这2个仓库的值

  • https://unpkg.com/browse/core-js@3.26.0/
  • https://github.com/zloirock/core-js/tree/master/packages/core-js

根据报错,是少了'es.array.flat-map''es.object.values',加上去

legacy({ //babel,兼容老浏览器,但是体积会大80%
      // targets: ['defaults', 'not IE 11']
        targets: ['chrome 52'],
        additionalLegacyPolyfills: ['regenerator-runtime/runtime'],
        renderLegacyChunks: true,
        modernPolyfills:[
            'es.array.flat-map',
            'es.object.values'
        ],
        polyfills: [
            'es.object.values',
            'es.array.flat-map'
        ]
    })
  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Unhandled error during execution of setup function 是一个错误消息,通常出现在使用Vue.js框架进行开发时。它表示在执行组件的setup函数期间发生了错误。 根据你提供的引用内容,有两个可能的原因导致这个错误: 1. 版本问题:根据的引用内容,你降低了Vue.js的版本到3.3后,问题得到了解决。这表明可能是你使用的Vue.js版本与其他依赖库或插件不兼容导致的。为了解决这个问题,你可以尝试将Vue.js版本升级或降级到与其他依赖库或插件兼容的版本。 2. 兼容性问题:根据[3]的引用内容,你提到了使用了 @vitejs/plugin-legacyds 这个兼容工具库。这个工具库可以帮助解决一些旧版本的浏览器不支持的问题。如果你在项目中使用了这个工具库,并且出现了 Unhandled error during execution of setup function 错误,可能是该工具库的配置或使用方式不正确导致的。你可以查阅该工具库的官方文档,确保正确配置和使用。 综上所述,要解决 Unhandled error during execution of setup function 错误,你可以尝试以下几个步骤: 1. 确认Vue.js的版本与其他依赖库或插件兼容。根据你的具体情况,考虑升级或降级Vue.js版本。 2. 如果你使用了 @vitejs/plugin-legacyds 这个兼容工具库,确保正确配置和使用。参考官方文档,检查你的配置是否正确。 3. 检查你的代码逻辑和语法,尤其是在组件的setup函数中。确保没有语法错误或逻辑问题导致的错误。 4. 如果以上步骤都不起作用,尝试搜索相关错误信息的解决方案。你可以参考Vue.js官方文档、社区论坛或其他开发者的讨论来找到可能的解决方案。 请注意,以上步骤仅供参考,具体解决方法可能因具体情况而异。建议你根据你的项目和错误信息的具体情况来选择合适的解决方案。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值