vue.js - Event from a child component isn't detected -
based on tree view sample (https://vuejs.org/v2/examples/tree-view.html) on vue , reorganize code followings: in parent component
<ul> <item class="item" :model="treedata" v-on:changetype="changetypeevent"></item> </ul> ... <script> ... methods: { changetypeevent: function () { console.log('incoming event ' ) } ... </script>
in child component,
<li> <div :class="{bold: isfolder}" @click="toggle" @dblclick.once="changetype"> {{model.name}} <span v-if="isfolder">[{{open ? '-' : '+'}}]</span> </div> <ul v-show="open" v-if="isfolder"> <item class="item" v-for="model in model.children" v-bind:key="model.name" :model="model"> </item> <li class="add" @click="addchild">+</li> </ul> </li> ... <script> export default { name: 'item', props: { model: object }, data: function () { return { open: false } }, computed: { isfolder: function () { return this.model.children && this.model.children.length } }, methods: { toggle: function () { if (this.isfolder) { this.open = !this.open } }, changetype: function () { if (!this.isfolder) { console.log('double click ' + this.model.name) this.$emit('changetype') } }, } } </script>
for reason, event not detected in parent component. have working code same structure. why doesn't work in case?
html attribute names case insensitive (https://www.w3.org/tr/2012/wd-html-markup-20120315/documents.html#case-insensitivity), therefore vue transforms event listener on parent pascal-case changetype
kebab-case change-type
template:
<ul> <item class="item" :model="treedata" v-on:change-type="changetypeevent"></item> </ul>
vue documentation: https://vuejs.org/v2/guide/components.html#camelcase-vs-kebab-case
Comments
Post a Comment