Skip to content

Instantly share code, notes, and snippets.

@VisionYi
Last active November 17, 2020 06:03
Show Gist options
  • Save VisionYi/f899d9032230e470e4fab7a057fd469b to your computer and use it in GitHub Desktop.
Save VisionYi/f899d9032230e470e4fab7a057fd469b to your computer and use it in GitHub Desktop.
<template>
<div v-for="(item, index) in valueSync" :key="item.id">
name: <input type="text" v-model="item.name" @input="syncPropValue"> <!-- add event -->
years: <input type="number" v-model="item.years" @input="syncPropValue"> <!-- add event -->
<button @click="removePepole(index)">remove</button>
</div>
</template>
<script>
export default {
//...
data() {
return {
valueSync: this.value
}
},
medthods: {
removePepole(index) {
this.valueSync.splice(index, 1)
this.syncPropValue()
},
// 讓 valueSync 觸發事件同步 value 傳遞到父組件
syncPropValue(value) {
this.$emit('input', this.valueSync)
},
},
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment