Vue2中引入使用ElementUI的教程詳解
1 安裝
推薦使用 npm 的方式安裝,它能更好地和 webpack打包工具配合使用。(本項目使用安裝方式)
npm i element-ui -S
也可以使用其他的包管理起進(jìn)行安裝:
# Yarn $ yarn add element-ui # pnpm $ pnpm install element-ui
2 引入
ElementUI分為全局引入和按需引入兩種方式,一般在工程項目中,如果使用全局引入,則項目初始化時會導(dǎo)致不必要的資源加載,為提升項目性能,建議進(jìn)行按需引入。以下我們對兩種引入方式進(jìn)行介紹。
2.1 全局引入
2.1.1 引入
在 main.js 中寫入以下內(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這個Babel插件。這樣,你可以只引入你實(shí)際使用的組件和它們的樣式,從而減小項目體積和構(gòu)建時間。
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)上圖效果,按需引入時需要將使用的所有組件都引入進(jìn)來,代碼如下:
<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>
按需引入組件,組件全部名稱詳見官網(wǎng);
3 總結(jié)
通常情況下,若是對性能沒有要求時,可以使用全局導(dǎo)入方式引入所有組件,若對頁面加載性能有要求,則最好使用按需加載方式引入組件,以防多余的資源加載增加頁面初始化耗時。
到此這篇關(guān)于Vue2中引入使用ElementUI的教程詳解的文章就介紹到這了,更多相關(guān)Vue2 ElementUI內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
用Vue.extend構(gòu)建消息提示組件的方法實(shí)例
本篇文章主要介紹了用Vue.extend構(gòu)建消息提示組件的方法實(shí)例,具有一定的參考價值,有興趣的可以了解一下2017-08-08從0到1構(gòu)建vueSSR項目之node以及vue-cli3的配置
這篇文章主要介紹了從0到1構(gòu)建vueSSR項目之node以及vue-cli3的配置,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-03-03vue全局自定義指令-元素拖拽的實(shí)現(xiàn)代碼
這篇文章主要介紹了面板拖拽之vue自定義指令,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下2019-04-04vue+element實(shí)現(xiàn)動態(tài)換膚的示例代碼
本文主要介紹了vue+element實(shí)現(xiàn)動態(tài)換膚的示例代碼,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-09-09vue里面v-bind和Props 利用props綁定動態(tài)數(shù)據(jù)的方法
今天小編就為大家分享一篇vue里面v-bind和Props 利用props綁定動態(tài)數(shù)據(jù)的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-08-08