一、出现的问题
elementui按需引入官网有毒,是这么写的,但我发现一直报错,估计是版本原因吧,官网写死了。
运行时报错:
二、正确的按需引入方式
1、首先按照官网所说安装插件
npm install babel-plugin-component -D
2、找到babel.config.js文件(找不到自己建一个与package.json同级)
module.exports = {
"plugins": [
[
"component",
{
"libraryName": "element-ui",
"styleLibraryName": "theme-chalk"
}
]
]
}
3、后面的可以按官网的来了
- 接下来,如果你只希望引入部分组件,比如 Button 和 Select,那么需要在 main.js 中写入以下内容:
import Vue from 'vue';
import { Button, Select } from 'element-ui';
import App from './App.vue';
Vue.component(Button.name, Button);
Vue.component(Select.name, Select);
/* 或写为
* Vue.use(Button)
* Vue.use(Select)
*/
new Vue({
el: '#app',
render: h => h(App)
});
其他的请参考官网:https://element.eleme.cn/#/zh-CN/component/quickstart