简体   繁体   English

使用 Webpack 和 ES6 的项目中出现奇怪的错误

[英]Strange error in project with Webpack and ES6

I am not an expert in webpack, please help me understand what the error is.我不是 webpack 的专家,请帮助我了解错误是什么。 I need to attach an event handler this way, and I don't understand what I'm doing wrong.我需要以这种方式附加一个事件处理程序,我不明白我做错了什么。 This colleague wrote this webpack config, I am still not familiar with the webpack.这位同事写了这个 webpack 配置,我对 webpack 仍然不熟悉。

 class Store { constructor(storeParams) { this.products = storeParams.products; this.container = document.querySelector('.js-goods'); } renderProducts() { const productTemplate = document.querySelector('.js-goods-temp'); for (let i = 0; i < this.products.length; i++) { const item = productTemplate.content.cloneNode(true); const id = this.products[i].id; const titleElem = item.querySelector('.js-title'); const descrElem = item.querySelector('.js-info'); const imageElem = item.querySelector('.js-img'); const priceElem = item.querySelector('.js-price'); const btnAddElem = item.querySelector('.js-add'); titleElem.innerHTML = this.products[i].title; descrElem.innerHTML = this.products[i].descr; imageElem.src = this.products[i].image; priceElem.innerHTML = this.products[i].price; btnAddElem.setAttribute('data-id', id); this.container.appendChild(item); btnAddElem.addEventListener('click', function () { console.log(this.products[i].id) // When I try to hang an event handler this way, an error is output to the console - see below }) } } }

goods-item.js:1 Uncaught Error: Module build failed (from./node_modules/babel-loader/lib/index.js): TypeError: G:\firststep\store\src\components\goods-item\goods-item.js: path.inShadow is not a function at ReplaceSupers.Function (G:\firststep\store\node_modules\babel-helper-replace-supers\lib\index.js:50:15) at NodePath._call (G:\firststep\store\node_modules@babel\traverse\lib\path\context.js:53:20) at NodePath.call (G:\firststep\store\node_modules@babel\traverse\lib\path\context.js:40:17) at NodePath.visit (G:\firststep\store\node_modules@babel\traverse\lib\path\context.js:88:12) at TraversalContext.visitQueue (G:\firststep\store\node_modules@babel\traverse\lib\context.js:112:16) at TraversalContext.visitMultiple (G:\firststep\store\node_modules@babel\traverse\lib\context.js:79:17) at TraversalContext.visit (G:\firststep\store\node_modules@babel\traverse\lib\context.js:138:19) at Function.traverse.node (G:\f goods-item.js:1 未捕获错误:模块构建失败(来自./node_modules/babel-loader/lib/index.js):TypeError: G:\firststep\store\src\components\goods-item\goods-item .js: path.inShadow 不是 ReplaceSupers.Function (G:\firststep\store\node_modules\babel-helper-replace-supers\lib\index.js:50:15) 在 NodePath._call 的 function firststep\store\node_modules@babel\traverse\lib\path\context.js:53:20) 在 NodePath.call (G:\firststep\store\node_modules@babel\traverse\lib\path\context.js:40: 17) 在 NodePath.visit (G:\firststep\store\node_modules@babel\traverse\lib\path\context.js:88:12) 在 TraversalContext.visitQueue (G:\firststep\store\node_modules@babel\traverse\ lib\context.js:112:16) 在 TraversalContext.visitMultiple (G:\firststep\store\node_modules@babel\traverse\lib\context.js:79:17) 在 TraversalContext.visit (G:\firststep\store\ node_modules@babel\traverse\lib\context.js:138:19) 在 Function.traverse.node (G:\f irststep\store\node_modules@babel\traverse\lib\index.js:80:17) at NodePath.visit (G:\firststep\store\node_modules@babel\traverse\lib\path\context.js:95:18) at TraversalContext.visitQueue (G:\firststep\store\node_modules@babel\traverse\lib\context.js:112:16) at TraversalContext.visitSingle (G:\firststep\store\node_modules@babel\traverse\lib\context.js:84:19) at TraversalContext.visit (G:\firststep\store\node_modules@babel\traverse\lib\context.js:140:19) at Function.traverse.node (G:\firststep\store\node_modules@babel\traverse\lib\index.js:80:17) at NodePath.visit (G:\firststep\store\node_modules@babel\traverse\lib\path\context.js:95:18) at TraversalContext.visitQueue (G:\firststep\store\node_modules@babel\traverse\lib\context.js:112:16) at TraversalContext.visitMultiple (G:\firststep\store\node_modules@babel\traverse\lib\context.js:79:17) at eval (webpack:///./src/components/goods-item/goods-item.js?:1:7) at Object../src/components/g irststep\store\node_modules@babel\traverse\lib\index.js:80:17) 在 NodePath.visit (G:\firststep\store\node_modules@babel\traverse\lib\path\context.js:95:18)在 TraversalContext.visitQueue (G:\firststep\store\node_modules@babel\traverse\lib\context.js:112:16) 在 TraversalContext.visitSingle (G:\firststep\store\node_modules@babel\traverse\lib\context. js:84:19) 在 TraversalContext.visit (G:\firststep\store\node_modules@babel\traverse\lib\context.js:140:19) 在 Function.traverse.node (G:\firststep\store\node_modules@ babel\traverse\lib\index.js:80:17) 在 NodePath.visit (G:\firststep\store\node_modules@babel\traverse\lib\path\context.js:95:18) 在 TraversalContext.visitQueue (G :\firststep\store\node_modules@babel\traverse\lib\context.js:112:16) 在 TraversalContext.visitMultiple (G:\firststep\store\node_modules@babel\traverse\lib\context.js:79:17)在 eval (webpack:///./src/components/goods-item/goods-item.js?:1:7) 在 Object../src/components/g oods-item/goods-item.js ( http://localhost:9000/js/app.min.js:96:1 ) at webpack_require ( http://localhost:9000/js/app.min.js:20:30 ) at eval (webpack:///./src/static/js/index.js?:3:18) at Object../src/static/js/index.js ( http://localhost:9000/js/app.min.js:108:1 ) at webpack_require ( http://localhost:9000/js/app.min.js:20:30 ) at http://localhost:9000/js/app.min.js:84:18 at http://localhost:9000/js/app.min.js:87:10 webpack_require ( http ://localhost:9000/js/app.min.js:20 :30 ) 在 eval (webpack:///./src/static/js/index.js?:3:18) 在 Object../src/static/js/index.js ( http://localhost:9000 /js/app.min.js:108:1 ) 在webpack_require ( http://localhost:9000/js/app.min.js:20:30 ) 在http://localhost:9000/js/app.min .js:84:18http://localhost:9000/js/app.min.js:87:10

solved the problem - set another preset @babel/preset-env instead babel-preset-env ,解决了这个问题 - 设置另一个预设@babel/preset-env而不是babel-preset-env

thanks to the advice of @loganfsmyth感谢@loganfsmyth 的建议

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM