【问题标题】:Pass event from one component to other components将事件从一个组件传递到其他组件
【发布时间】:2019-03-11 07:51:04
【问题描述】:

我真的是 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>

【问题讨论】:

  • 你能提供更多代码吗?
  • @boussadjrabrahim 添加了代码示例!

标签: vue.js vuejs2


【解决方案1】:

在 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
})

通过这种方式,您可以轻松地在各处分发/捕获事件。

祝你好运! :)

【讨论】:

  • 你可以在index.js中做Vue.prototype.$eventBus = new Vue()
  • 是的,有很多方法可以做到这一点?
【解决方案2】:

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

// 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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-25
    • 1970-01-01
    • 1970-01-01
    • 2021-10-31
    • 2018-04-16
    相关资源
    最近更新 更多