總結(jié)Vue Element UI使用中遇到的問(wèn)題
基于 vue2.0 的 element-ui 框架,使用起來(lái)還是很方便的,非常適合快速開(kāi)發(fā),但是在做自己的項(xiàng)目中還是會(huì)碰到這樣那樣的問(wèn)題,有些問(wèn)題官方文檔并不是很詳盡,以下是我在使用 element-ui 過(guò)程中一些常用的或碰到的一些問(wèn)題筆記。
一、DateTimePicker 日期選擇范圍為當(dāng)前時(shí)間以及當(dāng)前時(shí)間之前<template> <div><el-date-picker size='small' clearable :picker-options='pickerOptions' v-model='dateRange' type='daterange' value-format='yyyy-MM-dd' range-separator='至' start-placeholder='開(kāi)始日期' end-placeholder='結(jié)束日期'></el-date-picker> </div></template><script> export default {data () { return {pickerOptions: { disabledDate (time) {return time.getTime() > Date.now() }},dateRange: [] }} }</script>
還有一種情況就是,只能選取當(dāng)前時(shí)間之后的時(shí)間,包括時(shí)分秒,若選擇的時(shí)間小于當(dāng)前時(shí)間,就會(huì)自動(dòng)的填充成當(dāng)前的時(shí)分秒。這時(shí)可以配合watch監(jiān)聽(tīng)屬性或事件來(lái)處理。
<template> <div><el-date-picker size='small' clearable type='daterange' v-model='dateRange' :picker-options='pickerOptions' value-format='yyyy-MM-dd' range-separator='至' start-placeholder='開(kāi)始日期' end-placeholder='結(jié)束日期'></el-date-picker> </div></template><script> export default {data () { return {pickerOptions: { disabledDate (time) {return time.getTime() < Date.now() - 1 * 24 * 3600 * 1000 }},dateRange: [] }},watch: { dateRange (val) { //此處也可以替換成change事件var st = new Date(val) * 1000 / 1000if (st < Date.now()) { this.dateRange = new Date()} }} }</script>二、DateTimePicker 日期選擇范圍數(shù)組的拆分
項(xiàng)目中碰到的需求:type 為 daterange 的日期選擇器所綁定的值 date 是一個(gè)數(shù)組,但是后端接收的參數(shù)開(kāi)始日期和結(jié)束日期是分開(kāi)的,回顯時(shí)返回的數(shù)據(jù)也是分開(kāi)的
創(chuàng)建 arrayUtil.js 文件
// arrayUtil.js/** * @description 安全的獲取數(shù)組對(duì)應(yīng)下標(biāo)數(shù)據(jù) * @param { Array } arr * @param { int } index */export const saveGet = (arr, index) => { if( arr & Array.isArray(arr)) {return arr[index]; } else {return undefined; }}
在 .vue 文件中引入并調(diào)用
// .vue 文件import { saveGet } from ’./utils/arrayUtil’;<el-date-picker type='daterange' v-model='date' value-format='yyyy-mm-dd' format='yyyy-mm-dd' start-placeholder='開(kāi)始日期' end-placeholder='結(jié)束日期' style='width: 100%;'></el-date-picker>export default { data() {return { date: [] // 日期范圍} }, // 計(jì)算得到傳遞給后端的參數(shù)(拆分日期范圍數(shù)組) computed: {queryParams() { return {... ...fromDate: saveGet(this.form.date, 0),toDate: saveGet(this.form,date, 1),... ... };} },}
回顯的時(shí)候,后端返回的 fromDate 和 toDate 再拼成數(shù)組就可以了。
三、el-select 選擇器options的value/label采用拼接的方式<el-select placeholder='請(qǐng)選擇' filterable v-model='info' clearable > <el-option v-for='item in infoList' :key='info.id' :label='`name: ${item.name} - idNo: ${item.idNo}`' :value='item.id'> <span style='float: left'>{{ item.tableName }}</span> <span style='float: right; color: #8492a6; font-size: 13px'>{{ item.level }}</span> </el-option></el-select>
上述 v-model='info' 是從后端返回的選擇用戶 id,infoList 為所有用戶的信息,label 拼接了 用戶姓名 - 用戶idNo,回顯時(shí)要匹配過(guò)濾下然后再拼接顯示就行了。
顯示如下:
二次封裝 el-dialog 時(shí),關(guān)閉 dialog 出現(xiàn)如下錯(cuò)誤
具體代碼如下:
// 父組件<el-button type='primary' size='mini' @click='dialogVisible=true'>新 增</el-button><com-dialog :dialogVisible.sync='dialogVisible' @closeDialog='closeDialog'></com-dialog>// 子組件<template> <el-dialog :visible.sync='dialogVisible' @close='closeDialog'></template><script>export default { props: { dialogVisible: { type: Boolean, default: false } }, methods:{ //關(guān)閉Dialog closeDialog(){this.$emit(’update:closeDialog’, false); } },};</script>
出現(xiàn)錯(cuò)誤的原因是:子組件的關(guān)閉事件和父組件的關(guān)閉事件相沖突了,子組件的 props 屬性要由父組件來(lái)控制,不能直接修改 visible 的值。此處的 sync 修飾符相當(dāng)于 el-dialog 直接修改了父組件的值。所以把父組件和子組件的 .sync 去掉就可以了。
還有一種方法就是將 close 方法改成 before-close,具體代碼如下:
// 父組件<el-button type='primary' size='mini' @click='dialogVisible=true'>新 增</el-button><com-dialog :dialogVisible.sync='dialogVisible' @closeDialog='closeDialog'></com-dialog>// 子組件<template> <el-dialog :visible.sync='dialogVisible' :before-close='closeDialog'></template><script>export default { props: { dialogVisible: { type: Boolean, default: false } }, methods:{ //關(guān)閉Dialog closeDialog(){this.$emit(’closeDialog’, false); } },};</script>五、el-form-item的label自定義
要求在 form 表單的 label 中添加提示文字,具體顯示要求如下圖:
api文檔中form-item slot有個(gè)label屬性,用來(lái)自定義標(biāo)簽文本的內(nèi)容。實(shí)現(xiàn)如下:
<el-form-item prop='name'> <span slot='label'>用戶名<i>(支持字母、數(shù)字和特殊符號(hào))</i> </span> <el-input v-model='name'></el-input></el-form-item>
然后結(jié)合樣式修改下字體和顏色就可以了
六、el-input 使用clearable清除內(nèi)容時(shí)觸發(fā)校驗(yàn)提示form表單的el-input帶有輸入校驗(yàn),觸發(fā)方式trigger為blur,如果使用clearable清除內(nèi)容時(shí)不會(huì)觸發(fā)校驗(yàn)提示。文檔中el-input提供了focus()方法,在清除內(nèi)容的時(shí)候調(diào)用一下,在失去焦點(diǎn)時(shí)就會(huì)觸發(fā)校驗(yàn)了。具體實(shí)現(xiàn)如下:
<el-input placeholder='請(qǐng)輸入' v-model='form.name' clearable ref='nameRef' @clear='clearInput(’nameRef’)'></el-input> // 清除表單內(nèi)容事件clearInput (refName) { this.$refs[refName].focus()}
以上就是總結(jié)Vue Element UI使用中遇到的問(wèn)題的詳細(xì)內(nèi)容,更多關(guān)于Vue Element UI的資料請(qǐng)關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. vue跳轉(zhuǎn)頁(yè)面常用的幾種方法匯總2. XML 非法字符(轉(zhuǎn)義字符)3. CSS3實(shí)例分享之多重背景的實(shí)現(xiàn)(Multiple backgrounds)4. XML入門(mén)的常見(jiàn)問(wèn)題(三)5. 不要在HTML中濫用div6. ASP動(dòng)態(tài)include文件7. 父div高度不能自適應(yīng)子div高度的解決方案8. asp createTextFile生成文本文件支持utf89. Jquery使用原生AJAX方法請(qǐng)求數(shù)據(jù)10. el-input無(wú)法輸入的問(wèn)題和表單驗(yàn)證失敗問(wèn)題解決
