美文网首页
在Vue 项目中 添加element-ui 组件时,出现:Err

在Vue 项目中 添加element-ui 组件时,出现:Err

作者: PharkiLL | 来源:发表于2022-06-07 14:58 被阅读0次

这个错误的意思就是:在你的项目中,无法找到“babel-preset-es2015”依赖项。

正常情况,如果是我们自己平常使用,通常会直接将 element-ui的依赖全部引入,方式为:


import Vue from 'vue';
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
import App from './App.vue';
 
Vue.use(ElementUI);
 
new Vue({
  el: '#app',
  render: h => h(App)
});

通过这种方式,所引入的 element-ui,是一个完整的引入。缺点就是:对于真正项目中,会使得整个项目体积变大,引入了一些用不到的插件。

因此,为了解决实际项目中会出现这种情况,就有了一个“按需引入”

1.首先,安装:babel-plugin-component:

npm install babel-plugin-component -D
···
2.在目前的vue脚手架项目中,更改:babel.config.js中的配置,如:

module.exports = {
presets: [
'@vue/cli-plugin-babel/preset',
["es2015", { "modules": false }],
],
plugins:[
[
"component",
{
"libraryName": "element-ui",
"styleLibraryName": "theme-chalk"
}
]
]
}

但是这样子引入,会直接报错,出现“Error: Cannot find module 'babel-preset-es2015',因此,只需将上文中的“es2015” 改成 @babel/preset-env 即可。

然后,就可以对项目进行相应的部署。

3. 根据实际应用,引用需要使用的element ui 组件
 

相关文章

网友评论

      本文标题:在Vue 项目中 添加element-ui 组件时,出现:Err

      本文链接:https://www.haomeiwen.com/subject/lijemrtx.html