当前位置:   article > 正文

nextjs create-react-app 兼容ie11_script438: 对象不支持“closest”属性或方法

script438: 对象不支持“closest”属性或方法

1.next.js兼容ie11

nextjs自带对于ie11的兼容,所以自己的代码是能够兼容ie11的https://nextjs.org/docs/basic-features/supported-browsers-features,主要需要考虑第三方包中不兼容ie11的情况。

针对没有兼容ie11的三方包,我们需要配置babel进行转换:

a.nextjs提供了.bablerc.jshttps://nextjs.org/docs/advanced-features/customizing-babel-config

b.和 next.config.jshttps://nextjs.org/docs/api-reference/next.config.js/introduction,让用户自定义配置Babel和webpack

例如我选择在next.config.js的webpack配置中配置了babel loader进行转换:

  1. webpack: (config, options) => {
  2. config.module.rules.push({
  3. test: /\.m?js$/,
  4. exclude: function (modulePath) {
  5. return (
  6. /node_modules/.test(modulePath) &&
  7. !(
  8. /@react-spring/.test(modulePath) ||
  9. /ansi-to-react/.test(modulePath)
  10. )
  11. );
  12. },
  13. use: {
  14. loader: 'babel-loader',
  15. options: {
  16. presets: [
  17. [
  18. '@babel/preset-env',
  19. {
  20. 'targets': {
  21. 'browsers': ['last 2 versions', '> 0.1%', 'ie >= 11'],
  22. },
  23. },
  24. ],
  25. ],
  26. },
  27. },
  28. });
  29. return patchWebpackConfig(config, options);
  30. },

(备注:本地测试需要build之后再在ie中访问,dev模式由于存在websocket会导致ie不停报错)

2.create-react-app 兼容 ie11

主要做了一下几步:

a.如果使用了ts,需要在tsconfig.json 中将 "target" 设置成 es5 或者更低。

b.package.json中browserslist需要添加对应想要兼容的浏览器。

  1. "browserslist": {
  2.     "production": [
  3.       "ie 11",
  4.       //...others
  5.     ],
  6.     "development": [
  7.       "ie 11",
  8.       //...others
  9.     ]
  10.   }

3.直接build项目,并安装使用serve -s build在ie上运行项目,发现会报错: 对象不支持'assign'属性或方法。这是ie浏览器不兼容es6导致。这里推荐使用官方提供的 react-app-polyfill,然后在入口文件index.js中引入。
 

npm install react-app-polyfill --save


然后在项目入口文件第一行引入:

  1. import 'react-app-polyfill/ie11';
  2. // stable会导入browserslist中的浏览器目标,自动导入目标浏览器所需的polyfill
  3. import 'react-app-polyfill/stable';


正常情况下build之后就能兼容ie11运行了。


3.create-react-app各种其他报错:

a.

Objects are not valid as a react child....

(发生于 react 15.1.x之后)可能是react和react-dom的symbol的polyfill不一致造成的,请按照https://github.com/facebook/react/issues/8379排查,简单的解决方式为,在引入这两者之前,手动引入symbol:

import 'core-js/es/symbol';

b.

Function.prototype.toString: 'this' is not a Function object...

查找资料通过引入 webcomponents.js 解决 https://github.com/webcomponents/webcomponentsjs/

3.对象不支持“closest”属性或方法

在入口处,手动添加如下代码:

  1. if (!Element.prototype.matches) {
  2.   Element.prototype.matches =
  3.     (Element.prototype as any).msMatchesSelector || Element.prototype.webkitMatchesSelector;
  4. }
  5. if (!Element.prototype.closest) {
  6.   Element.prototype.closest = function (s: string) {
  7.     let el: Element | ParentNode | null = this;
  8.     do {
  9.       if (Element.prototype.matches.call(el, s)) return el;
  10.       el = el.parentElement || el.parentNode;
  11.     } while (el !== null && el.nodeType === 1);
  12.     return null;
  13.   };
  14. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/625008
推荐阅读
相关标签
  

闽ICP备14008679号