element UI 按需引入

1、npm安装【全局安装element】   

npm i element-ui -S

2、按需引入,借助  babel-plugin-component

        2.1、首先安装 babel-plugin-component

npm install babel-plugin-component -D

        2.2、将.babelrc修改为:【是项目中的babel.config.js文件】

{
  "presets": [["es2015", { "modules": false }]],
  "plugins": [
    [
      "component",
      {
        "libraryName": "element-ui",
        "styleLibraryName": "theme-chalk"
      }
    ]
  ]
}

        2.3、在main.js中引入需要的组件

比如:Button,Select

//引入需要的组件
import { Button, Select } from 'element-ui';

//使用组件
Vue.component(Button.name, Button);
Vue.component(Select.name, Select);

        2.4、或者挂载在原型上使用


//引入组件
import {MessageBox } from 'element-ui';

//挂载到原型上使用
Vue.prototype.$msgbox = MessageBox;
Vue.prototype.$alert = MessageBox.alert;

注意:如果依赖下载不了,在每行命令后面加上  

--legacy-peer-deps

猜你喜欢

转载自blog.csdn.net/m0_69644606/article/details/127881014