【发布时间】:2017-07-28 22:56:36
【问题描述】:
我需要将数据从子组件传递给父组件。代码很简单,但我不明白我缺少什么以及缺少什么。
export default {
name: 'TableOfContent',
props: ['itemGUID'], // is it nessosary?
methods: {
itemClick(itemGUID)
{
console.log(itemGUID);
this.$emit('newchapter', itemGUID) // passing GUID to parent
}
}
}
父模板:
<template>
<div class="Book" v-on:newchapter="foo(itemGUID)">
{{msg}}
</div>
</template>
<script>
import toc from './TableOfContent.vue'
export default {
name: 'mybook',
data () {
return {
msg: 'my main book'
}
},
methods:
{
foo(itemGUID)
{
console.log("GUID is: ", itemGUID);
}
},
components: {toc}
}
</script>
此代码不起作用:(
【问题讨论】:
-
你是不确定的吗?
-
你在模板哪里使用
TableOfContent,你能创建一个小提琴吗? Here 是在发出事件中发送参数的一个示例。 -
@Saurabh 看起来
foo()没有电话。我没有得到任何安慰。我会努力做到的。 -
tableofcontent 组件中应该是
this.$emit('newchapter', this.itemGUID)。 -
@Belmin 和
.this我在itemClick方法中变得未定义。
标签: vue.js