嗯,偶尔看看学习Vue 3技能啦,此前用过Vue 2作过一点东西,Vue 3已面世一段时间,因而乎,我来看看所遇到的问题是否在Vue 3中获得解决,首先,咱们来说讲一个例子在Vue 2中的实现,举个栗子吧,开发过程当中咱们只会用到省、市、区,这里的区也能够看作是3、四线城市中的县,若咱们想要基于县动态建立镇、村,更有甚者,在全国各地在镇下还划分不一样的区域,咱们经过Vue结合ElementUI来实现此例子数组
因为示例代码比较多,这里咱们首先直接看效果,以下:数据结构
具体代码以下所示,太多?忽略不看,咱们只讲解核心问题学习
<el-dialog :modal="dialogModal" :title="townTitle" @close="closeDialog" :visible.sync="dialogVisible" :close-on-click-modal="false" width="800px" top="10vh" center> <el-row> <el-form label-width="40px" ref="form" size="mini"> <el-form-item> <el-button size="small" @click="createMultipleTown" icon="el-icon-plus">添加镇</el-button> </el-form-item> <el-form-item v-for="(town, tindex) in form.towns" :key="tindex" style="border: 1px dashed #AAAAAA;margin:10px 0 20px 0;"> <el-row style="margin:20px 0 20px 0;"> <el-col :span="19"> <el-button type="danger" size="small" icon="el-icon-delete" @click="removeTown(tindex)">移除镇</el-button> </el-col> </el-row> <el-row style="margin:20px 0 20px 0;"> <el-col :span="4"> 镇名称 </el-col> <el-col :span="19"> <el-input maxlength="30" v-model="town.townName" placeholder="请输入镇名称"></el-input> </el-col> </el-row> <el-row style="margin-bottom:20px;"> <el-col :span="4"> 区域、村 </el-col> <el-col :span="20"> <el-radio-group v-model="town.option"> <el-radio @change="dynamicAddRegion(tindex)" label="添加区域"></el-radio> <el-radio label="添加村" @change="dynamicAddVillage(tindex)"></el-radio> </el-radio-group> </el-col> </el-row> <el-row v-for="(region, rindex) in town.regions" :key="rindex" style="margin-bottom:20px;"> <el-row> <el-col :span="4"> {{region.regionTitle}} </el-col> <el-col :span="5" style="margin-right:20px;" v-show="town.townRegionVisible"> <el-input size="small" maxlength="30" v-model="region.regionName" placeholder="请输入区域名称"></el-input> </el-col> <el-col :span="5" style="margin-right:20px;"> <el-tooltip class="item" effect="dark" content="输入村名称并回车,便可连续添加" placement="top"> <el-input size="small" maxlength="30" v-model="region.villageName" @keyup.enter.native="getVillage(tindex, rindex)" placeholder="请输入村名称"></el-input> </el-tooltip> </el-col> <el-col :span="5" v-show="!town.townRegionVillageVisible"> <el-button size="small" icon="el-icon-plus" @click="continueAddRegion(tindex)">追加区域</el-button> </el-col> <el-col :span="3" v-show="!town.townRegionVillageVisible" style="width:100px;"> <el-button size="small" type="danger" icon="el-icon-delete" @click="removeRegion(tindex, rindex)">移除区域</el-button> </el-col> </el-row> <el-row> <el-col :span="4"> <span> </span> </el-col> <el-col :span="20"> <el-tag :key="tagindex" v-for="(tag, tagindex) in region.tags" closable :disable-transitions="false" style="margin:10px 10px 0 0;" @close="handleClose(tindex, rindex, tagindex)"> {{tag}} </el-tag> </el-col> </el-row> </el-row> </el-form-item> </el-form> </el-row> <el-row v-show="saveButtonVisible"> <el-col :span="20"> <span> </span> </el-col> <el-col :span="2"> <el-button @click="save" type="primary">肯定</el-button> </el-col> <el-col :span="1"> <el-button @click="cancel">取消</el-button> </el-col> </el-row> </el-dialog>
直接看以下定义数据结构可得知,存在三层遍历,第一层遍历从镇开始,第二层遍历从镇下区域开始,最后一层遍历则是区域下的村(即上述标签)this
form: { areaId: 0, towns: [ { townName: '', regions: [{ regionTitle: '', regionName: '', villageName: '', tags: [] }] } ]}
在Vue 2中一直存在的问题则是没法监听数组,若我没记错的话,Vue 2是经过Object.defineProperty()来监听对象,也就是后台语言中对应的属性get和set方法。结合上述示例图和代码,当咱们输入村名称时,而后回车,则将村名称添加到村数组中去,而后经过el-tag标签进行遍历渲染spa
接下来问题来了,此时咱们想要删除村,因此点击村标签的删除图标,毫无疑问直接将区域下的村数组经过索引将其移除便可,可是,可是,根本没法移除,由于此时区域下的村是一个数组,Vue 2没法监听获得,也就是咱们在数组中给对应村移除时,但页面上没法同步删除,移除方法以下:代理
handleClose (tindex, rindex, tagindex) { let self = this let region = self.form.towns[tindex].regions[rindex] region.tags.splice(tagindex, 1) }
那么在Vue 2中如何解决这个问题呢?根据咱们的示例来看,咱们将输入的村名称即文本框绑定回车事件,而后将文本框绑定的模型数据添加到村数组中去,因此此时咱们伪装先再次在文本框上绑定一个“占位符”,而后紧接着将其置空,给Vue 2一种“错觉”刚才的数据没绑定上,因此上述删除标签方法,变成以下便可解决:code
handleClose (tindex, rindex, tagindex) { let self = this let region = self.form.towns[tindex].regions[rindex] region.tags.splice(tagindex, 1) // 添加文本准备添加“一个占位符”,以便于页面上能删除标签 region.villageName = '占位符' region.villageName = '' }
空闲之余,试了试Vue 3结合ElementPlus,这个问题获得了解决,在Vue 3中经过proxy(代理)方式监听全部属性,固然也就包括数组,而后在Vue 3中相关键盘事件等,好比回车,都已经过v-on:key.enter来绑定事件实现orm
本文也是作作小demo遇到的问题,特此记录下,其余倒没什么能够说的了,再会~~~~对象