好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

vue对于低版本浏览器兼容问题的解决思路

准备

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

工具库

@vitejs/plugin-legacyds

官方 唯一指定的兼容工具库,使用方式官网都有了

进阶 使用

问题例子

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

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

@H_ 360 _58@
[Vue warn]: Unhandled error during execution of  watcher  callback 
  at <Van config > 
  at <App>

[Vue warn]: Unhandled error during execution of  SETUP  function 
  at <VanConfig> 
  at <App>

Uncaught Ty PE Error: Object.values( .. .).flatMap is not a function\n\t/vite test /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 -i ssue.vuejs. org /?repo=vuejs/core 
  at <VanConfig> 
  at <App>

[Vue Router warn]: uncaught error during route navigation:

{}

Uncaught (in p rom ise)  {"n am e": "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  PR omise 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/plu gin -legacy'

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

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

https://unpkg.com/ br owse/core-js@3.26.0/ https://gith ub .com/zloirock/core-js/tree/master/packages/core-js

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

legacy({ //babel,兼容老浏览器,但是体积会大80%
      // t arg ets: [' defaults ', 'not IE 11']
        targets: ['c hr ome 52'],
        additionalLegacyPolyfills: ['regenerator-runtime/runtime'],
        renderLegacyChunks: true,
        modernPolyfills:[
            'es.array.flat-map',
            'es.object.values'
        ],
        polyfills: [
            'es.object.values',
            'es.array.flat-map'
        ]
    })

总结

到此这篇关于vue对于低版本浏览器兼容问题的解决思路的 文章 就介绍到这了,更多相关vue低版本浏览器兼容内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

您可能感兴趣的文章: 解决Vue2.0自带浏览器里无法打开的原因(兼容处理) vue-cli3项目在IE浏览器打开兼容问题及解决 详解Vue Cli浏览器兼容性实践

总结

以上是 为你收集整理的 vue对于低版本浏览器兼容问题的解决思路 全部内容,希望文章能够帮你解决 vue对于低版本浏览器兼容问题的解决思路 所遇到的问题。

如果觉得 网站内容还不错, 推荐好友。

查看更多关于vue对于低版本浏览器兼容问题的解决思路的详细内容...

  阅读:99次