代码之家  ›  专栏  ›  技术社区  ›  oliverbj

虚拟用户.js-从子级向父级发出事件

  •  0
  • oliverbj  · 技术社区  · 7 年前

    我不能 $emit 从子组件到其父组件的事件。

    Results.vue ( 孩子 ):

    <a href="#" v-on:click="sendResultValues"></a>
    
    // 
    
    methods: {
        sendResultValues: function () {
            this.$emit('send-result-values', 'carrier');
        }
    },
    

    <a> $发射 触发事件:

    enter image description here 但是,没有收到任何信息控制台.log我的代码如下(家长):

    Input.vue ( ):

    <search-results></search-results> //Results.vue component
    <search-popover v-on:send-result-values="showResultData"></search-popover>
     //
    methods: {
        showResultData: function () {
            console.log("Data received from child: ")
        }
     },
    
    2 回复  |  直到 7 年前
        1
  •  4
  •   Hammerbot    7 年前

    你需要听电视上的节目 search-results search-popover .

    <search-results v-on:send-result-values="showResultData"></search-results>
    <search-popover></search-popover>
    
    methods: {
        showResultData: function () {
            console.log("Data received from child: ")
        }
     },
    
        2
  •  0
  •   dobby    7 年前

    有更多的方法来解决这个问题。

    1. 你可以用vuex.js

    vm.$emit('test', 'hi'); 
    vm.$on('test', function (msg) { console.log(msg) }); // => "hi"