webpack react 錯誤整理

一、ERROR in ./src/entry.jsreact

Module build failed: SyntaxErrorwebpack

解決方法:web

安裝babel-preset-react,  npm install --save-dev babel-preset-reactnpm

 

修改webpack配置文件,添加preset選項babel

 

二、ERROR in bundle.js from UglifyJsreact-router

Unexpected token: punc ()) [bundle.js:25884,14]app

在添加並使用react-router-dom後,編譯報錯
dom

解決方法:安裝babel-preset-es2015 ,而後配置.babelrc文件函數

具體用法查看:https://babeljs.io/docs/plugins/preset-es2015/ui

 

三、Warning: It looks like you're using a minified copy of the development build of React.

When deploying React apps to production, make sure to use the production build which skips development warnings and is faster. See https://fb.me/react-minification for more details.

根據 https://reacttraining.com/react-router/web/example/basic給出的例子,編譯沒報錯,頁面打開時,控制檯出現警告,點擊連接時報錯

 Uncaught DOMException: Failed to execute 'pushState' on 'History': A history state object with URL 'file:///E:/about' cannot be created in a document with origin 'null' and URL

解決警告方法:修改webpack配置文件,添加以下內容

網上暫時沒找到直接的解決方法,只能一步步查找緣由,將Route上的exact去掉後,發現頁面能夠顯示Home內容,因此路由是沒問題的,問題在Link上面

關於Route exact,官方文檔給出的說明以下,看了以後仍是沒太明白o(╯□╰)o,大體理解就是不加exact,默認首頁顯示patch爲「/」的頁面,加了以後exact後,匹配路徑時要求更嚴格一些

 四、使用箭頭函數時編譯報錯

解決方法:安裝babel-preset-stage-1

npm install babel-preset-stage-1  --save-dev

修改配置文件,添加stage-1選項

 參考:http://blog.csdn.net/wq_static/article/details/51330780

 

五、cannot read push of undefined

使用this.context.router.push("about")進行頁面跳轉時報錯

解決方法:添加以下內容

 

六、Uncaught TypeError: n.context.router.push is not a function

使用版本以下:

解決方法:

將 this.context.router.push("about") 換成

this.context.router.history.push("about")

參考:this-context-router-push-is-not-a-function

相關文章
相關標籤/搜索