I'm pretty new to Vue.js, and I use ES6 syntax with vue-class-component
.(我是Vue.js的新手,并且使用ES6语法和vue-class-component
。)
I'm having a problem while trying to emit an event from a child to its parent.(我在尝试将事件从孩子发送给父母时遇到问题。)
I followed the logic of the default Vue.js syntax but can't seem to have my parent catch an event emitted by the the child.(我遵循了默认Vue.js语法的逻辑,但似乎无法让我的父母抓住孩子发出的事件。) Code:(码:)
Child Component(子组件)
I attached a click event listener on every <li>
, which calls a function that emits an event.(我在每个<li>
上附加了一个click事件监听器,该监听器调用了一个发出事件的函数。) The event listener is defined on the parent.(事件侦听器是在父级上定义的。)
export default class HorizontalNavigation {
handleClick (e) {
e.preventDefault();
// console.log(e.target.dataset.section);
this.$emit('ChangeView', e.target.dataset.section);
}
render (h) {
return (
<div class={ style.horizontalNavigation } >
<div class='sections-wrapper'>
<div class={ style.sections }>
<ol>
<li><a on-click={ this.handleClick } href='#1' data-section='1' class='selected'>We are</a></li>
<li><a on-click={ this.handleClick } href='#2' data-section='2'>Services</a></li>
<li><a on-click={ this.handleClick } href='#3' data-section='3'>Cases</a></li>
<li><a on-click={ this.handleClick } href='#4' data-section='4'>Studio</a></li>
<li><a on-click={ this.handleClick } href='#5' data-section='5'>Career</a></li>
<li><a on-click={ this.handleClick } href='#6' data-section='6'>Contact</a></li>
</ol>
<span class='filling-line' aria-hidden='true'></span>
</div>
</div>
</div>
);
}
}
Parent Component(父组件)
export default class ViewsContainer {
ChangeView (data) {
console.log(data);
}
render (h) {
return (
<div class={ style.restrictOverflow } >
<HorizontalNavigation />
<main class={ style.viewsContainer } on-ChangeView={ this.ChangeView } >
<SingleView dataSection='weare' id='1' class='selected' />
<SingleView dataSection='services' id='2' />
<SingleView dataSection='cases' id='3' />
<SingleView dataSection='studio' id='4' />
<SingleView dataSection='career' id='5' />
<SingleView dataSection='contact' id='6' />
</main>
</div>
);
}
}
Following the vue.js syntax, I should be able to listen to the child's event from the parent, by emitting the event from the element, but the parent doesn't seem to catch the event.(按照vue.js语法,我应该能够通过从元素发出事件来侦听来自父级的子级事件,但是父级似乎没有捕获到该事件。)
Where am I going wrong?(我要去哪里错了?)
ask by Lou translate from so
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…