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è)趕rc/components下新建一個(gè)組件,用來(lái)寫(xiě)我們的展示組件,然后在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 /> <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='操作' 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' @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 :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 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-itemprop='dates' // 需要驗(yàn)證的字段 需要對(duì)應(yīng)rules里面的鍵label='日期':label- :rules='[ {required: true, message: ’必須選擇一個(gè)日期’, trigger: ’blur’},]' // 也可以直接寫(xiě)在item里面驗(yàn)證 也可以全放在rules。我這里是采取了兩種方式 > <el-date-picker v-model='form.dates' 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)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. idea設(shè)置提示不區(qū)分大小寫(xiě)的方法2. docker容器調(diào)用yum報(bào)錯(cuò)的解決辦法3. CentOS郵件服務(wù)器搭建系列—— POP / IMAP 服務(wù)器的構(gòu)建( Dovecot )4. HTTP協(xié)議常用的請(qǐng)求頭和響應(yīng)頭響應(yīng)詳解說(shuō)明(學(xué)習(xí))5. .NET SkiaSharp 生成二維碼驗(yàn)證碼及指定區(qū)域截取方法實(shí)現(xiàn)6. IntelliJ IDEA設(shè)置默認(rèn)瀏覽器的方法7. IntelliJ IDEA創(chuàng)建web項(xiàng)目的方法8. ASP.NET MVC通過(guò)勾選checkbox更改select的內(nèi)容9. 原生JS實(shí)現(xiàn)記憶翻牌游戲10. django創(chuàng)建css文件夾的具體方法
