Vue使用Element實(shí)現(xiàn)增刪改查+打包的步驟
在我們進(jìn)行項(xiàng)目開(kāi)發(fā)期間,避免不了使用各式各樣的組件,Element
是由餓了么公司前端團(tuán)隊(duì)開(kāi)源。樣式精美、組件齊全、易于上手。
效果:
組件使用
我們利用vue-cli
創(chuàng)建一個(gè)項(xiàng)目,然后只需要安裝element-ui
即可
安裝:npm i element-ui -S
然后在main.js中引用一下樣式即可,可以選擇按需加載,我們這邊因?yàn)槭茄菔疽幌拢圆蝗ミM(jìn)行調(diào)整,項(xiàng)目中如果使用到的組件不多,可以選擇按需加載。
main.js
import Vue from 'vue'; import App from './App.vue'; import ElementUI from 'element-ui'; import 'element-ui/lib/theme-chalk/index.css'; Vue.config.productionTip = false; Vue.use(ElementUI); new Vue({ render: h => h(App), }).$mount('#app')
然后我們?cè)?code>src/components下新建一個(gè)組件,用來(lái)寫我們的展示組件,然后在app.vue
中導(dǎo)入即可
app.vue
<template> <div id="app"> <Creator content1="憧憬"/> </div> </template> <script> import Creator from './components/Creator/Creator'; export default { name: 'app', components: { Creator } } </script>
我們首先先使用表格,將數(shù)據(jù)展示出來(lái)
Creator.vue
<template> <div class="Creator"> <el-row :gutter="20"> <el-col :span="6"> <el-input v-model="content" placeholder="請(qǐng)輸入內(nèi)容"></el-input> </el-col> <el-col :span="2"> <el-button type="primary">搜索</el-button> </el-col> </el-row> <div style="height: 20px"/> <el-row :gutter="10" type="flex" justify="center"> <el-col :span="14"> <el-table :data="tableData" // 聲明列表使用的數(shù)據(jù) :key="'zip'" // 聲明每一行的key border style="width: 100%"> <el-table-column fixed prop="date" label="日期" width="150"> </el-table-column> <el-table-column prop="name" // 對(duì)應(yīng)tableData里面的需要展示的鍵 label="姓名" width="120"> </el-table-column> <el-table-column prop="province" label="省份" width="120"> </el-table-column> <el-table-column prop="city" label="市區(qū)" width="120"> </el-table-column> <el-table-column prop="address" label="地址" width="300"> </el-table-column> <el-table-column prop="zip" label="郵編" width="120"> </el-table-column> <el-table-column fixed="right" label="操作" width="160" v-slot="scope" // 獲取每一行的數(shù)據(jù) > <template> <el-button @click="handleCreate(scope.row)" type="text" size="small">添加</el-button> <el-popconfirm confirmButtonText='好的' cancelButtonText='不用了' icon="el-icon-info" iconColor="red" title="這是一段內(nèi)容確定刪除嗎?" @onConfirm="handleDelete(scope.row)" > <el-button slot="reference" type="text" size="small">刪除</el-button> </el-popconfirm> </template> </el-table-column> </el-table> </el-col> </el-row> <el-dialog title="添加用戶" :visible.sync="dialogFormVisible"> // rules指定表單驗(yàn)證規(guī)則 <el-form :model="form" status-icon ref="ruleForm" :rules="rules" :label-position="'right'"> <el-row :gutter="10"> <el-col :span="11"> <el-form-item prop="name" label="姓名" :label-width="formLabelWidth"> <el-input style="width: 200px" v-model="form.name" autocomplete="off"></el-input> </el-form-item> </el-col> </el-row> <el-row :gutter="10"> <el-col :span="11"> <el-form-item prop="dates" // 需要驗(yàn)證的字段 需要對(duì)應(yīng)rules里面的鍵 label="日期" :label-width="formLabelWidth" :rules="[ {required: true, message: '必須選擇一個(gè)日期', trigger: 'blur'}, ]" // 也可以直接寫在item里面驗(yàn)證 也可以全放在rules。我這里是采取了兩種方式 > <el-date-picker v-model="form.dates" align="right" type="date" placeholder="選擇日期" format="yyyy 年 MM 月 dd 日" // 展示數(shù)據(jù)的格式 value-format="yyyy-MM-dd" // 聲明點(diǎn)擊后的數(shù)據(jù)格式 :picker-options="pickerOptions"> </el-date-picker> </el-form-item> </el-col> </el-row> </el-form> <div slot="footer" class="dialog-footer"> <el-button @click="dialogFormVisible = false">取 消</el-button> <el-button type="primary" @click="onOk">確 定</el-button> </div> </el-dialog> </div> </template> <script> export default { props: { content1: {required: true, type: String} }, data() { // 自定義驗(yàn)證函數(shù) 給name驗(yàn)證 const validatName = (rule, value, callback) => { if (!value) return callback(new Error('名字不能為空')); if (value.length <= 0) return callback(new Error('最少一個(gè)字符')); return callback(); }; return { content: this.content1, tableData: [ { date: '2016-05-02', name: '王小虎', province: '上海', city: '普陀區(qū)', address: '上海市普陀區(qū)金沙江路 1518 弄', zip: 200331 }, { date: '2016-05-04', name: '王小虎', province: '上海', city: '普陀區(qū)', address: '上海市普陀區(qū)金沙江路 1517 弄', zip: 200332 }, { date: '2016-05-01', name: '王小虎', province: '上海', city: '普陀區(qū)', address: '上海市普陀區(qū)金沙江路 1519 弄', zip: 200333 }, { date: '2016-05-03', name: '王小虎', province: '上海', city: '普陀區(qū)', address: '上海市普陀區(qū)金沙江路 1516 弄', zip: 200334 }], formLabelWidth: '120px', // 控制模態(tài)是否展示 dialogFormVisible: false, form: { name: '', dates: null, }, // 對(duì)picker組件的擴(kuò)展 pickerOptions: { // 將之后的時(shí)間禁用 不然選擇 disabledDate(time) { return time.getTime() > Date.now(); }, // 增加 今天 昨天 一周前的快速選項(xiàng) shortcuts: [{ text: '今天', onClick(picker) { picker.$emit('pick', new Date()); } }, { text: '昨天', onClick(picker) { const date = new Date(); date.setTime(date.getTime() - 3600 * 1000 * 24); picker.$emit('pick', date); } }, { text: '一周前', onClick(picker) { const date = new Date(); date.setTime(date.getTime() - 3600 * 1000 * 24 * 7); picker.$emit('pick', date); } }] }, // 定義輸入規(guī)則 rules: { name: [ // 指定驗(yàn)證函數(shù) 觸發(fā)時(shí)機(jī)。這個(gè)是失去焦點(diǎn)觸發(fā) {validator: validatName, trigger: 'blur'} ], }, }; }, methods: { onOk() { // 使用ref進(jìn)行驗(yàn)證 validate傳入一個(gè)函數(shù) 返回一個(gè)驗(yàn)證是否成功的bool值 this.$refs['ruleForm'].validate((valid) => { if (valid) { const { name, dates } = this.form; // 避免zip重復(fù) zip++ const maxZip = Math.max(...this.tableData.map(item => item.zip)) + 1; const obj = { name, date: dates, province: '北京', city: '普陀區(qū)', address: '上海市普陀區(qū)金沙江路 1518 弄', zip: maxZip }; // push到數(shù)據(jù)里面 this.tableData.push(obj); // 將模態(tài)隱藏 this.dialogFormVisible = false; } else { return false; } }); }, // 刪除數(shù)據(jù) handleDelete(row) { this.tableData.map((item, index) => { if (item.zip === row.zip) { this.tableData.splice(index, 1); } }); }, handleCreate() { // 模態(tài)展示 this.dialogFormVisible = true; } } }; </script>
一套基本的增刪改查就可以了呀,Vue有一套admin模版,開(kāi)箱即用。
vue-element-admin
非常不錯(cuò),大家可以去使用一下子
打包
默認(rèn)打包的話會(huì)導(dǎo)致靜態(tài)資源引用存在問(wèn)題,打開(kāi)一片空白,所以我們打包前需要先配置一下靜態(tài)資源
在package.json
這個(gè)文件同級(jí)的目錄,新建一個(gè)vue.config.js
,加入如下配置
/** * Created By 憧憬 */ module.exports = { publicPath: './' // 靜態(tài)資源目錄配置為./ 當(dāng)前目錄 };
以上就是Vue使用Element實(shí)現(xiàn)增刪改查+打包的步驟的詳細(xì)內(nèi)容,更多關(guān)于vue 增刪改查+打包的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Vue中addEventListener()?監(jiān)聽(tīng)事件案例講解
這篇文章主要介紹了Vue中addEventListener()?監(jiān)聽(tīng)事件案例講解,包括語(yǔ)法講解和事件冒泡或事件捕獲的相關(guān)知識(shí),本文結(jié)合示例代碼給大家講解的非常詳細(xì),需要的朋友可以參考下2022-12-12在Vue3中實(shí)現(xiàn)四種全局狀態(tài)數(shù)據(jù)的統(tǒng)一管理的方法
在開(kāi)發(fā)中,通常遇到四種全局狀態(tài)數(shù)據(jù):異步數(shù)據(jù)、同步數(shù)據(jù),傳統(tǒng)的Vue3使用不同機(jī)制處理這些數(shù)據(jù),而Zova框架通過(guò)Model機(jī)制來(lái)統(tǒng)一管理,簡(jiǎn)化了數(shù)據(jù)處理流程,提高了代碼的可維護(hù)性,本文介紹在Vue3中實(shí)現(xiàn)四種全局狀態(tài)數(shù)據(jù)的統(tǒng)一管理的方法,感興趣的朋友一起看看吧2024-10-10使用vue-cli創(chuàng)建vue項(xiàng)目介紹
這篇文章介紹了使用vue-cli創(chuàng)建vue項(xiàng)目的方法,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-01-01詳解TypeScript+Vue 插件 vue-class-component的使用總結(jié)
這篇文章主要介紹了TypeScript+Vue 插件 vue-class-component的使用總結(jié),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-02-02vue使用v-if v-show頁(yè)面閃爍,div閃現(xiàn)的解決方法
在頁(yè)面層次結(jié)構(gòu),數(shù)據(jù)較多的時(shí)候,用v-if或者v-show就會(huì)出現(xiàn)div閃現(xiàn),或者部分閃爍的結(jié)果。怎么處理這樣的問(wèn)題呢,下面小編給大家?guī)?lái)了vue使用v-if v-show頁(yè)面閃爍,div閃現(xiàn)的解決方法,一起看看吧2018-10-10vue通過(guò)指令(directives)實(shí)現(xiàn)點(diǎn)擊空白處收起下拉框
這篇文章主要介紹了vue通過(guò)指令(directives)實(shí)現(xiàn)點(diǎn)擊空白處收起下拉框,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-12-12vue實(shí)現(xiàn)無(wú)縫滾動(dòng)手摸手教程
這篇文章主要為大家介紹了vue實(shí)現(xiàn)無(wú)縫滾動(dòng)手摸手教程示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03