$emit(update: prop, "newPropVulue") 這個模式,使子組件向父組件傳達:更新屬性,並拋出新的屬性值
.sync 修飾符 是父組件中修改prop值得修飾符iview
一:什麼地方須要用到.sync修飾符呢
當子傳父,父級有兩數據,而沒有v-modal時
例如iview的Tree組件中:
父級:this
<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 }, }
子級:code
<Tree :data="folderTree" :render="renderFunc"></Tree>
子級方法中的寫法:component
let updateListName = isFolder ? 'folderList' : 'fileList' this.$emit(`update:${updateListName}`, list)
二:.sync與$emit的寫法問題
使用.sync修飾符,即it
// this.$emit('update:folder-tree',100); //無效 this.$emit('update:folderTree',100); //有效 //...... <folder-tree v-bind:folder-tree.sync="test"></folder-tree>
與不使用.sync,即event
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>