将事件从一个组件传递到其他组件

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了将事件从一个组件传递到其他组件相关的知识,希望对你有一定的参考价值。

我是Vue的新手,似乎无法了解事件如何从一个组件传递到其他组件。我目前正在使用v-blur,我想模糊除了点击之外的每个组件。我想通过在单击原始组件时将事件传递给其他组件,我可以获得所需的效果。任何帮助深表感谢!

// Parent.vue
<template>
  <div id="Parent">
    <child-one @toggle-blur="toggleChildBlur"/>
    <child-two @toggle-blur="toggleChildBlur"/>
    <child-three @toggle-blur="toggleChildBlur"/>
  </div>
</template>

<script>
import ChildOne from './ChildOne'
import ChildTwo from './ChildTwo'
import ChildThree from './ChildThree'

export default {
  name: 'Parent',
  components: {
    ChildOne, ChildTwo, ChildThree
  },
  methods: {
    toggleChildBlur () {
      // Blur every child except the clicked one?
    }
  },
  data () {
    return {}
  }
}
</script>  

// ChildOne.vue, basically the same for two and three aswell
<template>
  <div id="child-one" v-blur="blurConfig" @click="$emit('toggle-blur')"></div>
</template>

<script>
export default {
  name: 'ChildOne',
  methods: {
    toggleBlur () {
      this.blurConfig.isBlurred = !this.blurConfig.isBlurred;
    }
  },
  data () {
    return {
      blurConfig: {
        isBlurred: false,
        opacity: 0.3,
        filter: 'blur(1.2px)',
        transition: 'all .3s linear'
      }
    }
  }
}
</script>
答案

在Vue中发送的事件向一个方向传播:孩子⇒父母。如果你有一个组件P(父)和子C1(子1)和C2(子2),则无法在C1中触发事件并将其发送到C2。它会去P.

如果你有非常嵌套的结构(很多级别)并且你真的需要这样做,那么最简单的方法就是在不属于显示列表的东西上发送和监听事件,即全局。非常典型的解决方案是使用所谓的“事件总线” - 一个单独的虚拟Vue实例,仅用于事件。这是关于Global Event Bus in Vue的完整教程。

它看起来像这样:

// in some global file
const EventBus = new Vue();

// in GC1 (parent -> child 1 -> grand child 1)
EventBus.$emit('someEvent', 'some-data')

// in GC5 (parent -> child 3 -> grand child 5)
EventBus.$on('someEvent', function(data) {
  console.log(data) // 'some-data
})

这样您就可以轻松地在整个地方发送/捕获事件。

祝好运! :)

另一答案

我想出了一种方法来获得我想要的效果。我的解决方案可能不是很可扩展,但现在可以使用!我从发射器传递子索引并循环以模糊除了单击的子索引之外的每个组件。

// ChildOne.vue
// Basically the same for two and three as well except sending corresponding index
// on click event.

// Click event is now sending the index of the component to know which one got clicked.
<template>
  <div id="child-one" @click="$emit('toggle-blur', 0)"></div>
</template>

// Parent.vue

// Every child component now gets their separate blur config.
// When child is clicked the index of the child now gets sent to help skip and blur
// the other components.
<template>
  <div id="parent">
    <child-one v-blur="blurConfig[0]" @toggle-blur="toggleChildBlur"/>
    <child-two v-blur="blurConfig[1]" @toggle-blur="toggleChildBlur"/>
    <child-three v-blur="blurConfig[2]" @toggle-blur="toggleChildBlur"/>
  </div>
</template>

<script>
import ChildOne from './ChildOne'
import ChildTwo from './ChildTwo'
import ChildThree from './ChildThree'

export default {
  name: 'Parent',
  components: {
    ChildOne, ChildTwo, ChildThree
  },
  methods: {
    toggleChildBlur (childIndex) {
      // Unblur if click event is outside focused component
      if (this.blurConfig[childIndex].isBlurred) {
        for (let i = 0; i < this.blurConfig.length; i++) {
          this.blurConfig[i].isBlurred = false
        }
      } else {
        for (let i = 0; i < this.blurConfig.length; i++) {
          if (i !== childIndex) {
            this.blurConfig[i].isBlurred = !this.blurConfig[i].isBlurred
          }
        }
      }
    }
  },
  data () {
    return {
      // Blur settings for each component
      blurConfig: [
        {
          isBlurred: false,
          opacity: 0.2,
          filter: 'blur(1.2px)',
          transition: 'all .2s linear'
        },
        {
          isBlurred: false,
          opacity: 0.2,
          filter: 'blur(1.2px)',
          transition: 'all .2s linear'
        },
        {
          isBlurred: false,
          opacity: 0.2,
          filter: 'blur(1.2px)',
          transition: 'all .2s linear'
        }
      ]
    }
  }
}
</script>

以上是关于将事件从一个组件传递到其他组件的主要内容,如果未能解决你的问题,请参考以下文章

将数据从底部工作表对话框片段传递到片段

是否可以在没有点击事件及其方法的情况下使用服务将数据从一个组件传递到另一个组件?

Vue - 无法使用 $emit 事件将数据从组件传递到父级

如何使用新的导航架构组件从扩展 BroadcastReceiver 的类导航到片段

当我在 React js 中使用 forwardRef 将 Ref 从函数组件传递到其他函数组件时出现 TypeError,

将 prop 从组件传递到堆栈导航器