Vue2中引入使用ElementUI的教程詳解
1 安裝
推薦使用 npm 的方式安裝,它能更好地和 webpack打包工具配合使用。(本項(xiàng)目使用安裝方式)
npm i element-ui -S
也可以使用其他的包管理起進(jìn)行安裝:
# Yarn $ yarn add element-ui # pnpm $ pnpm install element-ui
2 引入
ElementUI分為全局引入和按需引入兩種方式,一般在工程項(xiàng)目中,如果使用全局引入,則項(xiàng)目初始化時(shí)會(huì)導(dǎo)致不必要的資源加載,為提升項(xiàng)目性能,建議進(jìn)行按需引入。以下我們對(duì)兩種引入方式進(jìn)行介紹。
2.1 全局引入
2.1.1 引入
在 main.js 中寫(xiě)入以下內(nèi)容:
import Vue from 'vue';
import ElementUI from 'element-ui';
//樣式文件需要單獨(dú)引入
import 'element-ui/lib/theme-chalk/index.css';
import App from './App.vue';
Vue.use(ElementUI);
new Vue({
el: '#app',
render: h => h(App)
});
以上代碼便完成了 Element 的引入。
2.1.2 使用
引入完成之后就可以使用組件了,如下示例為使用container組件和button組件:
效果如下:

代碼如下:
<template>
<el-container>
<el-header>Header</el-header>
<el-container>
<el-aside width="200px">Aside</el-aside>
<el-container>
<el-main>
Main
<el-button type="primary">按鈕</el-button>
</el-main>
<el-footer>Footer</el-footer>
</el-container>
</el-container>
</el-container>
</template>
<script>
export default {};
</script>
<style scoped>
.el-header,
.el-footer {
background-color: #b3c0d1;
color: #333;
text-align: center;
line-height: 60px;
}
.el-aside {
background-color: #d3dce6;
color: #333;
text-align: center;
line-height: 200px;
}
.el-main {
background-color: #e9eef3;
color: #333;
text-align: center;
line-height: 160px;
}
body > .el-container {
margin-bottom: 40px;
}
.el-container:nth-child(5) .el-aside,
.el-container:nth-child(6) .el-aside {
line-height: 260px;
}
.el-container:nth-child(7) .el-aside {
line-height: 320px;
}
</style>
2.2 按需引入
可以使用babel-plugin-component這個(gè)Babel插件。這樣,你可以只引入你實(shí)際使用的組件和它們的樣式,從而減小項(xiàng)目體積和構(gòu)建時(shí)間。
2.2.1 引入
首先,安裝 babel-plugin-component:
npm install babel-plugin-component -D
然后,將 .babelrc 或者babel.config.js文件修改為:
{
"presets": [["es2015", { "modules": false }]],
"plugins": [
[
"component",
{
"libraryName": "element-ui",
"styleLibraryName": "theme-chalk"
}
]
]
}
2.2.2 使用
若想實(shí)現(xiàn)上圖效果,按需引入時(shí)需要將使用的所有組件都引入進(jìn)來(lái),代碼如下:
<template>
<el-container>
<el-header>Header</el-header>
<el-container>
<el-aside width="200px">Aside</el-aside>
<el-container>
<el-main>
Main
<el-button type="primary">按鈕</el-button>
</el-main>
<el-footer>Footer</el-footer>
</el-container>
</el-container>
</el-container>
</template>
<script>
import Vue from "vue";
import { Button, Container, Header, Aside, Main, Footer } from "element-ui";
Vue.use(Button);
Vue.use(Container);
Vue.use(Aside);
Vue.use(Main);
Vue.use(Footer);
Vue.use(Header);
export default {};
</script>
<style scoped>
.el-header,
.el-footer {
background-color: #b3c0d1;
color: #333;
text-align: center;
line-height: 60px;
}
.el-aside {
background-color: #d3dce6;
color: #333;
text-align: center;
line-height: 200px;
}
.el-main {
background-color: #e9eef3;
color: #333;
text-align: center;
line-height: 160px;
}
body > .el-container {
margin-bottom: 40px;
}
.el-container:nth-child(5) .el-aside,
.el-container:nth-child(6) .el-aside {
line-height: 260px;
}
.el-container:nth-child(7) .el-aside {
line-height: 320px;
}
</style>
按需引入組件,組件全部名稱(chēng)詳見(jiàn)官網(wǎng);
3 總結(jié)
通常情況下,若是對(duì)性能沒(méi)有要求時(shí),可以使用全局導(dǎo)入方式引入所有組件,若對(duì)頁(yè)面加載性能有要求,則最好使用按需加載方式引入組件,以防多余的資源加載增加頁(yè)面初始化耗時(shí)。
到此這篇關(guān)于Vue2中引入使用ElementUI的教程詳解的文章就介紹到這了,更多相關(guān)Vue2 ElementUI內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
用Vue.extend構(gòu)建消息提示組件的方法實(shí)例
本篇文章主要介紹了用Vue.extend構(gòu)建消息提示組件的方法實(shí)例,具有一定的參考價(jià)值,有興趣的可以了解一下2017-08-08
Vant的安裝和配合引入Vue.js項(xiàng)目里的方法步驟
這篇文章主要介紹了Vant的安裝和配合引入Vue.js項(xiàng)目里的方法步驟,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-12-12
從0到1構(gòu)建vueSSR項(xiàng)目之node以及vue-cli3的配置
這篇文章主要介紹了從0到1構(gòu)建vueSSR項(xiàng)目之node以及vue-cli3的配置,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-03-03
vue全局自定義指令-元素拖拽的實(shí)現(xiàn)代碼
這篇文章主要介紹了面板拖拽之vue自定義指令,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-04-04
在Vue項(xiàng)目中使用snapshot測(cè)試的具體使用
這篇文章主要介紹了在Vue項(xiàng)目中使用snapshot測(cè)試的具體使用,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-04-04
使用 vue.js 構(gòu)建大型單頁(yè)應(yīng)用
本文給大家詳細(xì)介紹了如何使用使用 vue.js腳手架工具vue-cli構(gòu)建大型單頁(yè)應(yīng)用的方法,非常的實(shí)用,有需要的小伙伴可以參考下2018-02-02
vue+element實(shí)現(xiàn)動(dòng)態(tài)換膚的示例代碼
本文主要介紹了vue+element實(shí)現(xiàn)動(dòng)態(tài)換膚的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09
vue里面v-bind和Props 利用props綁定動(dòng)態(tài)數(shù)據(jù)的方法
今天小編就為大家分享一篇vue里面v-bind和Props 利用props綁定動(dòng)態(tài)數(shù)據(jù)的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-08-08

