vue project - ElementUI按需引入

安装按需引入包

npm install babel-plugin-component

babel.config.js:

按需引入配置

module.exports = {
  presets: [
    '@vue/cli-plugin-babel/preset',
    //1.ElementUI 按需引入配置
    ["@babel/preset-env", { "modules": false }]
  ],
  //2.ElementUI 按需引入配置
  "plugins": [
    [
      "component",
      {
        "libraryName": "element-ui",
        "styleLibraryName": "theme-chalk"
      }
    ]
  ]
}

src/element/index.js:

把将要使用到的组件写到一个js文件中

// 导入自己需要的组件
import {Select,Option,OptionGroup,Input,Tree,Dialog,Row,Col,Button} from 'element-ui'
const element = {
  install: function (Vue) {
    Vue.use(Select)
    Vue.use(Option)
    Vue.use(OptionGroup)
    Vue.use(Input)
    Vue.use(Tree)
    Vue.use(Dialog)
    Vue.use(Row)
    Vue.use(Col)
    Vue.use(Button)
  }
}
export default element

scr/main.js:

注册使用element组件(src/element/index.js)

import Vue from 'vue'
import App from './App.vue'

//导入将要使用的组件
import element from './element/index'
//注册将要使用的组件
Vue.use(element)

Vue.config.productionTip = false

new Vue({
  render: h => h(App),
}).$mount('#app')

 

出错时参考文章:(13条消息) vue2引用element2的两种方式_vue2使用element_丹丹的小跟班的博客-CSDN博客

posted on 2022-05-26 17:24  每天积极向上  阅读(95)  评论(0编辑  收藏  举报

导航