使用$emit传参:如何同时接收父组件和子组件的参数?

需求描述:组件内部发射事件并且有参数传递出来,然后监听该事件时,事件处理程序又同时需要获取组件外面传进来的参数
举个例子:

tag-input组件监听change事件

<template>
  <div class="tag-input-wrapper">
    <input
      v-model="tag"
      @change="emitChangeTag"
    >
  </div>
</template>
  methods: {
    ...,
    emitChangeTag () {
      this.$emit('changeTag', this.tag)
    }
  }

在调用tag-input时需要传递index,修改tags数组的第index项

    <tag-input
      v-for="(tag, index) in tags"
      :key="index"
      @changeTag="onChangeTag(index, arguments)"
    ></tag-input>
methods: {
    ...,
    onChangeTag () {
      const index = arguments[0]
      const tag = arguments[1][0]
      this.tags[index] = tag
    }
}

猜你喜欢

转载自blog.csdn.net/dongdaxiaopenyou/article/details/82954101
今日推荐