引用mui.js
報錯信息:
Uncaught TypeError: 'caller', 'callee', and 'arguments' properties may not be accessed on strict mode functions or the arguments objects for calls to them
方案一
原因:babel在將js文件轉碼為ES5時,默認使用嚴格模式,而在嚴格模式下,為了安全起見是不能用caller,callee,arguments等屬性的。
解決: 修改bablerc文件的配置,讓項目忽略mui.js的轉碼,完美解決我的問題
"ignore": [
"./src/assets/lib/mui/js/mui.js"
]
此外,如果設置了語法檢查,也會各種檢查第三方js的語法錯誤,從而報錯。通過修改eslintignor文件,將對應目錄下的js忽略即可
方案二
移除嚴格模式:
npm i babel-plugin-transform-remove-strict-mode -D
.babelrc添加transform-remove-strict-mode
"plugins": [
"transform-vue-jsx",
"transform-runtime",
"transform-remove-strict-mode"
]
以上解決方案用到我的vue-cli項目中會報錯:"export 'default' (imported as 'mui') was not found in '../../assets/lib/mui/js/mui.js'
但同一個例子沒用vue-cli,用方案二卻完美解決。
補充
也有看到博主說添加 transform-remove-strict-mode
並將 transform-runtime
移除。個人覺得用 ignore
只是忽略了指定第三方js的轉碼,如果移除了 transform-runtime
,這就相當於移除了一個項目的代碼轉換插件,嗯,做的很絕啊。
關於babel將高級語法轉為ES5,推薦看一下:Babel之babel-polyfill、babel-runtime、transform-runtime詳解