vue項目中如何實現(xiàn)element-ui組件按需引入
element-ui組件按需引入
按需引入
1.借助 babel-plugin-component ,引入我們需要的組件,減少項目體積
npm install babel-plugin-component -D
2.修改 babel.config.js 的內容
//babel.config.js 全文內容如下
module.exports = {
presets: [
'@vue/cli-plugin-babel/preset'
],
plugins: [
[
'component',
{
libraryName: 'element-ui',
styleLibraryName: 'theme-chalk'
}
]
]
}3.創(chuàng)建文件 element.js(名字自定義)
// element.js 全文內容如下,按自己需要引入就好了
import Vue from 'vue'
import {
Button,
Form,
FormItem,
Input,
Message,
Container,
Header,
Aside,
Main,
Menu,
Submenu,
MenuItem,
Breadcrumb,
BreadcrumbItem,
Card,
Row,
Col,
Table,
TableColumn,
Switch,
Tooltip,
Pagination,
Dialog,
MessageBox,
Tag,
Tree,
Select,
Option,
Cascader,
Alert,
Tabs,
TabPane
} from 'element-ui'
Vue.use(Button)
Vue.use(Form)
Vue.use(FormItem)
Vue.use(Input)
Vue.use(Container)
Vue.use(Header)
Vue.use(Aside)
Vue.use(Main)
Vue.use(Menu)
Vue.use(Submenu)
Vue.use(MenuItem)
Vue.use(Breadcrumb)
Vue.use(BreadcrumbItem)
Vue.use(Card)
Vue.use(Row)
Vue.use(Col)
Vue.use(Table)
Vue.use(TableColumn)
Vue.use(Switch)
Vue.use(Tooltip)
Vue.use(Pagination)
Vue.use(Dialog)
Vue.use(Tag)
Vue.use(Tree)
Vue.use(Select)
Vue.use(Option)
Vue.use(Cascader)
Vue.use(Alert)
Vue.use(Tabs)
Vue.use(TabPane)
Vue.prototype.$message = Message
Vue.prototype.$confirm = MessageBox.confirm
4.最后在 main.js 中引入這個文件
//main.js 中添加下面這行代碼(路徑和文件名按自己的來) import './plugins/element.js'
完整引入
在 main.js 中添加如下代碼
import ElementUI from 'element-ui'; Vue.use(ElementUI);
vue項目搭建 + 引入element-ui
初始化單頁系統(tǒng)
在學習Vue的過程中,官方網(wǎng)站都是給了非常詳細的介紹,所以初始化大型單頁應用,官網(wǎng)給的參考資料地址:https://cn.vuejs.org/v2/guide/installation.html
1、NPM

2、命令行工具 (CLI)

3、具體操作步驟
【第一步】在004目錄下右鍵,然后選擇------在命令提示符中打開

【第二步】輸入npm install vue

【第三步】安裝命令行工具vue-cli:cnpm install vue-cli --global

【第四步】初始化項目的命令:vue init webpack 項目名

【第五步】進入項目命令:cd my-project

【第六步】運行項目:npm run dev

【第七步】訪問項目,如果一切成功,那么會出現(xiàn)下圖所示頁面,系統(tǒng)初始化成功

系統(tǒng)目錄介紹
1、經(jīng)過操作,項目my-project已經(jīng)初始化成功,目錄結構如下:

修改項目
1、修改App.vue,刪除無關內容

<template>
<div id="app">
<router-view/>
</div>
</template>
<script>
export default {
}
</script>
<style>
</style>
2、修改main.js代碼

// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import 'element-ui/lib/theme-chalk/index.css'
Vue.config.productionTip = false
/* eslint-disable no-new */
new Vue({
el: '#app',
router,
render: c=> c(App)
})
3、修改HelloWorld.vue組件中的代碼

ElementUI整合項目
在項目中,我們采用ElementUI作為系統(tǒng)框架,所以需要安裝ElementUI框架
ElementUI框架官網(wǎng)地址:http://element-cn.eleme.io/#/zh-CN/component/installation
1、安裝ElementUI: cnpm i element-ui -S
i: 安裝指令,全拼:install-S:生產環(huán)境,全拼:--save-D:開發(fā)環(huán)境,全拼:--save--dev-O:可選依賴,全拼:--save--optional-E:精確安裝指定模塊版本,全稱:--save--exact-g:全局安裝,全拼:--global

2、在main.js中引入ElementUI模塊

// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
Vue.config.productionTip = false
Vue.use(ElementUI)
/* eslint-disable no-new */
new Vue({
el: '#app',
router,
render: c => c(App)
})
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
vue?select?change事件如何傳遞自定義參數(shù)
這篇文章主要介紹了vue?select?change事件如何傳遞自定義參數(shù),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-04-04
關于elementUi表格合并行數(shù)據(jù)并展示序號
這篇文章主要介紹了關于elementUi表格合并行數(shù)據(jù)并展示序號,通過給table傳入span-method方法可以實現(xiàn)合并行或列,方法的參數(shù)是一個對象,感興趣的朋友可以學習一下2023-04-04
vue實現(xiàn)修改標簽中的內容:id class style
這篇文章主要介紹了vue實現(xiàn)修改標簽中的內容:id class style,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-07-07

