考虑到element-ui版本更新或者重新通过npm install安装包时,写入node_moduleselement-uilibelement-ui.common.js里面的patch会失效。
其实可以单独写入一个文件到项目目录里面,然后在webpack的打包入口文件里面,在引入element-ui之前引入。
1 `(function (window) { 2 try { 3 new MouseEvent('test'); 4 return false; // No need to polyfill 5 } catch (e) { 6 // Need to polyfill - fall through 7 } 8 9 // Polyfills DOM4 MouseEvent 10 var MouseEventPolyfill = function (eventType, params) { 11 params = params || { bubbles: false, cancelable: false }; 12 var mouseEvent = document.createEvent('MouseEvent'); 13 mouseEvent.initMouseEvent(eventType, 14 params.bubbles, 15 params.cancelable, 16 window, 17 0, 18 params.screenX || 0, 19 params.screenY || 0, 20 params.clientX || 0, 21 params.clientY || 0, 22 params.ctrlKey || false, 23 params.altKey || false, 24 params.shiftKey || false, 25 params.metaKey || false, 26 params.button || 0, 27 params.relatedTarget || null 28 ); 29 30 return mouseEvent; 31 } 32 33 MouseEventPolyfill.prototype = Event.prototype; 34 35 window.MouseEvent = MouseEventPolyfill; 36 })(window);`
- 源码目录 node_moduleselement-uilibelement-ui.common.js
- 参考 https://github.com/ElemeFE/element/issues/17404