element-ui 按需引入說明
1.新建vue項目
npm i -g @vue/cli
vue create my-app
cd my-app
2.安裝element-ui
yarn add element-ui
yarn add babel-plugin-component -D
3.修改 babel.config.js
若是沒有這個文件,建立一個便可
module.exports = {
presets: ["@vue/cli-plugin-babel/preset"],
plugins: [
[
"component",
{
libraryName: "element-ui",
styleLibraryName: "theme-chalk",
},
],
],
};
4.修改main.js
import Vue from 'vue'
import ElementUI from 'element-ui'
// 樣式文件仍是要所有引入,目前沒有找到好的方法
import 'element-ui/lib/theme-chalk/index.css'
import lang from 'element-ui/lib/locale/lang/zh-CN'
import locale from 'element-ui/lib/locale'
import App from './App.vue'
import router from './router'
import store from './store'
Vue.config.productionTip = false
// element-ui config
Object.entries(ElementUI).forEach(([key, component]) => {
if (/^[A-Z]+/.test(key)) {
switch (key) {
case 'Loading':
Vue.use(component.directive)
Vue.prototype.$loading = component.service
break
case 'Notification':
Vue.prototype.$notify = component
break
case 'Message':
Vue.prototype.$message = component
break
case 'MessageBox':
Vue.prototype.$msgbox = component
Vue.prototype.$alert = component.alert
Vue.prototype.$confirm = component.confirm
Vue.prototype.$prompt = component.prompt
break
default:
Vue.use(component)
}
}
})
Vue.prototype.$ELEMENT = { size: 'small', zIndex: 3000 }
locale.use(lang)
new Vue({
router,
store,
render: h => h(App)
}).$mount('#app')