当下公司在做一个媒体门户网站,后台由另一家公司使用java开发并提供接口,本人负责开发后台页面,使用的是vue-element-admin开发
下面说一下问题场景,在开发过程中有一个后台管理员角色页面,其中包含一个表单dialog,在其中使用了el-tree组件,相关 代码结构如下:
<div class="filter-container"> <el-button class="filter-item" style="margin-left: 10px;" v-waves @click="handleCreate" type="primary" icon="el-icon-edit">新增角色 </el-button> </div> <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible" width="50%"> <el-form :rules="rules" ref="dataForm" :model="temp" label-position="top" label-width="90px" style='width: 400px; margin-left:50px;'> <el-form-item label="选择权限" prop="sysPermission"> <el-tree ref="tree" :data="sysPermission" :props="formProps" show-checkbox @check-change="handleCheckChange" node-key="id"></el-tree> </el-form-item> </el-form> </el-dialog> 相关的js如下:
export default { name: 'sysRoleList', data() { return { tableKey: 0, list: null, total: null, listLoading: true, formLoading: true, listQuery: { page: 1, limit: 20, importance: undefined, title: undefined, type: undefined, sort: '+id' }, dialogFormVisible: false, dialogStatus: 'update', textMap: { update: '编辑角色', create: '新增角色' }, rules: { sysRoleName: [{required: true, message: '必须填写角色名称', trigger: 'blur'}] }, // 表单数据 temp: { id: '', sysPermissionList: [], sysRoleName: '' }, currentKeys: [], // 表单权限字段映射 formProps: { label: 'sysPermissionName' }, sysPermission: {} } }, created() { // 列表数据 // this.getList() // 获取所有权限 // this.findAllSysPermission() }, methods: { /* * todo:checkbox状态变更监听 * */ handleCheckChange(data, checked, indeterminate) { const idObj = {id: data.id} this.temp.sysPermissionList.push(idObj) }, resetTemp() { this.temp = { id: '', sysRoleName: '', sysPermissionList: [] } }, handleCreate() { this.resetTemp() this.dialogStatus = 'create' this.currentKeys = [] this.dialogFormVisible = true this.$nextTick(() => { this.$refs['dataForm'].clearValidate() this.$refs.tree.setCheckedKeys(this.currentKeys) }) }, handleUpdate(row) { this.resetTemp() this.dialogStatus = 'update' this.dialogFormVisible = true this.temp = Object.assign({}, row) // copy obj this.currentKeys = [] row.sysPermissionList.forEach((value, index) => { this.currentKeys.push(value[0]) }) this.$nextTick(() => { this.$refs['dataForm'].clearValidate() this.$refs.tree.setCheckedKeys(this.currentKeys) }) } } } 需求:
需要在每次编辑数据的时候触发<el-tree>的方法setCheckedKeys
问题:
之前没有把this.$refs.tree.setCheckedKeys()写在this.$nextTick的callback之中,因此会提示:
...