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