vue.js - Why Data gets leaked into sibling Instance of Component when Removed vue 2 -


i building following scenario. parent creates multi instances of child component. each child holds data via input field. child can ask removed , parent removes instance. far good. problem, instance removed, data gets passed/leaked next sibling instance , if instance holding data, gets moved other next-to-it instance. have reproduced on fiddle

or see below

    vue.component('child', {      	props:['data'],          template: `              <div>                  index# {{data}}: {{messages}}                  <input type="text" v-model="text" @keypress.enter="addmessage" placeholder="add data delete it">                  <button @click="addmessage">add</button>                  <button @click="$emit('delete-me')">delete</button>              </div>`,              data() {              	return {                	messages:[],                  text: ''                }              },              methods: {              	addmessage() {                	this.messages.push(this.text)                  this.text = ''                }              }      })            vue.component('parent', {          template: `              <div>                  keep adding new instances                   <button @click="newchild">new</button>                  <hr />                  <child v-for="(child, index) in children" key="index"                  v-on:delete-me="deletethisrow(index)""                  :data="child"                  ></child>              </div>`,              data() {              	return {                	children:[]                }          },          methods: {          	newchild() {            	this.children.push(this.children.length)            },            deletethisrow(index) {                  this.children.splice(index, 1);              }          }      })            new vue({          el: '#app',          template: `              <div>              		<parent />                                </div>          `,                    methods: {                        }      })                
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.3.3/vue.min.js"></script>  <div id="app"></div>

two mistakes in here:

  1. :key instead of key

reason: since dynamic values can change, vue should know change keep updated

  1. child instead of index

reason: not sure may because vue holds own copy of indices in virtual dom needs values only


Comments

Popular posts from this blog

python Tkinter Capturing keyboard events save as one single string -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

javascript - Z-index in d3.js -