(1)首先,安装 babel-plugin-component:
npm install babel-plugin-component -D
(2)配置项目中的.babelrc 如下:
{
"plugins": [
[
"component",
{
"libraryName": "element-ui",
"styleLibraryName": "theme-chalk"
}
]
]
}
(3)在src/新建文件夹:element/文件:index.js内容如下:
import Vue from 'vue'
import 'element-ui/lib/theme-chalk/index.css'
//element引入组件
import {
Row,
Col,
Progress,
Tabs,
TabPane,
Tag,
Select,
Option,
DatePicker,
TableColumn,
Table,
Form,
FormItem,
Input,
Button,
InputNumber,
Switch,
Radio,
RadioGroup,
Dialog,
MessageBox,
Checkbox,
CheckboxButton,
CheckboxGroup,
} from 'element-ui'
Vue.use(Row);
Vue.use(Input)
Vue.use(Form);
Vue.use(FormItem);
Vue.use(Col);
Vue.use(Progress);
Vue.use(Tabs);
Vue.use(TabPane);
Vue.use(Tag);
Vue.use(Select);
Vue.use(Option);
Vue.use(DatePicker);
Vue.use(TableColumn);
Vue.use(Table);
Vue.use(Button);
Vue.use(InputNumber);
Vue.use(Switch);
Vue.use(Radio);
Vue.use(RadioGroup);
Vue.use(Dialog);
Vue.use(Checkbox);
Vue.use(CheckboxButton);
Vue.use(CheckboxGroup);
Vue.prototype.$msgbox = MessageBox;
Vue.prototype.$alert = MessageBox.alert;
Vue.prototype.$confirm = MessageBox.confirm;
Vue.prototype.$prompt = MessageBox.prompt;
(4)在main.js中引入element/index如下
import './element/index'
简洁明了,看了好多前端大佬的按需引入自己总结的一套方法,如有更好的请多指教谢谢