简体   繁体   English

VueJS 中的父子通信

[英]Parent-child communication in VueJS

I have two Vue components.我有两个 Vue 组件。 The parent-component : parent-component

Vue.component('parent-component',{
        methods: {
            test: function(){
             alert('Option Selected');
            }
        },
        template: `
            <div><slot></slot></div>
        `
});

And the animals component: animals部分:

Vue.component('animals',{
        data: function(){
            return {
                selected: ''
            }
        },
        template: `
            <select @change="selectionChanged" v-model="selected">
                <slot></slot>
            </select>
        `,
        methods: {
            selectionChanged: function(){
                this.$emit('optionselected', this.selected);
            }
        }
 });

And here is my HTML:这是我的 HTML:

<div id="app">
        <parent-component @optionselected="test()">
            <animals>
                <option>Aardvark</option>
                <option>Bear</option>
                <option>Cat</option>
            </animals>
        </parent-component>
 </div>

I am trying to get the selected option from child component ( animals ) to the parent component ( parent-component ).我正在尝试将所选选项从子组件( animals )获取到父组件( parent-component )。 I am emitting the optionselected event from the child, but it looks like the parent component is not responding to that event, I mean the method test() is not being called at all.我正在从子组件发出optionselected事件,但看起来父组件没有响应该事件,我的意思是根本没有调用 test() 方法。 What am I doing wrong here?我在这里做错了什么?

Here is the JSFiddle Demo这是JSFiddle 演示

First, you need to add the listener to the animals component in your template.首先,您需要将侦听器添加到模板中的animals组件。

<animals @optionselected="test">

Second, it's important to remember that because you are using slots , the elements inside the slots are going to be evaluated in the scope of the component in which they are defined.其次,重要的是要记住,因为您使用的是插槽,所以插槽内的元素将在定义它们的组件范围内进行评估。 In this case, that scope is the Vue's scope, not the parent-component scope.在这种情况下,该范围是 Vue 的范围,而不是parent-component范围。 In order to allow the elements defined inside a slot to use the containing components data, methods, etc, you need to define a scoped slot .为了允许定义在槽内的元素使用包含的组件数据、方法等,您需要定义一个作用域槽 That being the case, your parent component would end up looking like this:在这种情况下,您的父组件最终将如下所示:

<div><slot :test="test"></slot></div>

And your Vue template becomes你的 Vue 模板变成

<parent-component>
  <template scope="{test}">
    <animals @optionselected="test">
      <option>Aardvark</option>
      <option>Bear</option>
      <option>Cat</option>
    </animals>
  </template>
</parent-component>

Here is the updated code.这是更新的代码。

 console.clear() Vue.component('parent-component', { methods: { test: function(option) { alert('Option Selected ' + option); } }, template: ` <div><slot :test="test"></slot></div> ` }); Vue.component('animals', { data: function() { return { selected: '' } }, template: ` <select @change="selectionChanged" v-model="selected"> <slot></slot> </select> `, methods: { selectionChanged: function() { this.$emit('optionselected', this.selected); } } }); new Vue({ el: "#app", });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.4.2/vue.js"></script> <div id="app"> <parent-component> <template scope="{test}"> <animals @optionselected="test"> <option>Aardvark</option> <option>Bear</option> <option>Cat</option> </animals> </template> </parent-component> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM