最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - In Vue.js, how can I emit multiple value from child to parent while parent's v-on pass another parameter? -

programmeradmin13浏览0评论

This is a code example.

Vueponent('button-counter', {
  template: '<button v-on:click="emit_event">button</button>',
  methods: {
    emit_event: function () {
      this.$emit('change', 'v1', 'v2', 'v3')  // Here I emit multiple value
    }
  },
})
new Vue({
  el: '#parent',
  data: {
    args: ""
  },
  methods: {
    change: function (...args) {
      this.args = args
      console.log(args)
    }
  }
})
<script src=".3.3/vuemon.js"></script>
<div id="parent">
  {{ args }} <br />
  <button-counter v-on:change="change(1234, $event)"></button-counter>
</div>

From the parent component, I want to get parameter pass by change() (in this example, 1234), but also every value emitted by child component. I try to use $event to catch the values child emit, however $event is only set up to the first value child emit (int this example, 'v1')

Is there any way to do that? I know I can emit an array to catch multiple value. But some library just emit multiple value.

This is the codepen for above example.

This is a code example.

Vue.component('button-counter', {
  template: '<button v-on:click="emit_event">button</button>',
  methods: {
    emit_event: function () {
      this.$emit('change', 'v1', 'v2', 'v3')  // Here I emit multiple value
    }
  },
})
new Vue({
  el: '#parent',
  data: {
    args: ""
  },
  methods: {
    change: function (...args) {
      this.args = args
      console.log(args)
    }
  }
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.3.3/vue.common.js"></script>
<div id="parent">
  {{ args }} <br />
  <button-counter v-on:change="change(1234, $event)"></button-counter>
</div>

From the parent component, I want to get parameter pass by change() (in this example, 1234), but also every value emitted by child component. I try to use $event to catch the values child emit, however $event is only set up to the first value child emit (int this example, 'v1')

Is there any way to do that? I know I can emit an array to catch multiple value. But some library just emit multiple value.

This is the codepen for above example. https://codepen.io/anon/pen/MmLEqX?editors=1011

Share Improve this question asked May 24, 2017 at 4:57 user2925565user2925565 1,3922 gold badges12 silver badges15 bronze badges 1
  • some library just emit multiple value - collect these values into an array or object, and pass it as the event. Mulpitle values are not supported here. – Egor Stambakio Commented May 24, 2017 at 5:22
Add a comment  | 

3 Answers 3

Reset to default 36

Define a handler that accepts the multiple parameters from the event and passes them along to the change method in addition to your static parameter.

<button-counter v-on:change="(...args)=>this.change(1234,...args)"></button-counter>

Alternatively

<button-counter v-on:change="(...args)=>this.change([1234,...args])"></button-counter>

And change your method to

change: function (args) {
  this.args = args
  console.log(args)
}

You can use destructuring syntax

this.$emit('change', { x:'v1', y:'v2', z: 'v3' })

And you can access these values like so

<button-counter @change="change"></button-counter>

methods: { change({x, y, z}) { .... } }

I would do this:

<button-counter v-on:change="change(1, ...arguments)">
发布评论

评论列表(0)

  1. 暂无评论