$emit(update: prop, "newPropVulue") 这个模式,使子组件向父组件传达:更新属性,并抛出新的属性值
.sync 修饰符 是父组件中修改prop值得修饰符
一:什么地方需要用到.sync修饰符呢
当子传父,父级有两数据,而没有v-modal时
例如iview的Tree组件中:
父级:
<folder-tree
:folder-list.sync="folderList"
:file-list.sync="fileList"
:folder-drop="folderDrop"
:file-drop="fileDrop"
:beforDelete="beforeDelete"
/>
import FolderTree from '_c/folder-tree'
export default {
components: {
FolderTree
},
}
子级:
<Tree :data="folderTree" :render="renderFunc"></Tree>
子级方法中的写法:
let updateListName = isFolder ? 'folderList' : 'fileList'
this.$emit(`update:${updateListName}`, list)
二:.sync与$emit的写法问题
使用.sync修饰符,即
// this.$emit('update:folder-tree',100); //无效
this.$emit('update:folderTree',100); //有效
//......
<folder-tree v-bind:folder-tree.sync="test"></folder-tree>
与不使用.sync,即
this.$emit('update:folder-tree',100); //有效
//this.$emit('update:folderTree',100); // 无效
//......
<folderTree v-bind:father-num="test" v-on:update:folder-tree="test=$event" ></folderTree>