So I've recently started working with Vue js. I'm attempting to dynamically add and remove Vue nodes. It's a bit difficult to describe the issue so I've created a demo to illustrate it.
Vue.component('context', {
data() {
return {
test: '<context></context>', //Dummy recursive data to illustrate issue
child: ''
}
},
methods: {
addChild() {
this.child = this.test
},
removeChild() {
this.child = ''
}
},
computed: {
dynamic() {
return Vue.compile(this.child)
},
style() {
return {
'background-color': '#' + randHex(6)
}
}
},
template: `
<div :style="style" @click="addChild" @click.shift="removeChild">
<component :is="dynamic"></component>
</div>
`
})
new Vue({
el: '#app'
})
function randHex(digits) {
let hex = Math.floor(Math.random() * Math.pow(16, digits)).toString(16)
return '0'.repeat(digits - hex.length) + hex
}
html,
body {
height: 100%;
overflow: hidden;
}
div {
width: 90%;
height: 90%;
}
<script src="https://unpkg.com/vue@2.4.3/dist/vue.js"></script>
<p>Click on the block to add, Shift-Click ro remove. Why does shift clicking always remove all inner blocks and not just the ones as children to the shift clicked block?</p>
<div id="app">
<context></context>
</div>
Above you will see that Clicking on the colored rectangles adds a inner child as intended. However when you shift click on a rectangle it not only removes its children, but ALL children! (Even ones that are parents to the current node.)
Initially I had thought the click event was "bleeding through" to the lower elements, I did however create a bit more complex test that offset the elements position to not be above one another, this still produced the same strange behavior.
Any help on understanding / resolving this issue would be greatly appreciated.