是因为vue-cli默认禁止了commonjs语法
/* istanbul ignore next */
if (typeof module !== 'undefined' && module.exports) {
module.exports = fecha;
} else if (typeof define === 'function' && define.amd) {
define(function () {
return fecha;
});
} else {
main.fecha = fecha;
}
module.exports == undefined,导致if (typeof module !== 'undefined' && module.exports){}不生效,fecha报错
首先引入:cnpm install --save-dev @babel/plugin-transform-modules-commonjs
然后在 babel.config.js 文件中配置:plugins.push('@babel/plugin-transform-modules-commonjs');
const plugins = ['@vue/babel-plugin-transform-vue-jsx']
// 生产环境移除console
if (process.env.NODE_ENV === 'production') {
plugins.push('transform-remove-console')
}
plugins.push('@babel/plugin-transform-modules-commonjs');
module.exports = {
plugins: plugins,
presets: [
'@vue/app'
]
}