【问题标题】:Listen for Foundation event in Vue.js?在 Vue.js 中监听 Foundation 事件?
【发布时间】:2017-09-26 23:45:10
【问题描述】:

我正在 Vue.js 中构建一个应用程序,其一般结构如下:

<app>
    <filters-component></filters-component>
    <div class="off-canvas-content">
        <nav-component></nav-component>
        <div class="row card-grid">
            <card-component v-for="item in items">
                <modal-component v-if="launchModal === true"></modal-component>
            </card-component>
        </div>
    </div>
</app>

只有当 launchModal 的数据元素设置为 true 时(单击按钮启动模式后),这才允许我在 DOM 上呈现模式。这很好用,但是当它关​​闭时我需要做相反的事情。

根据 Foundation 的文档,Reveal(模态)组件在关闭时应该发出一个名为 closed.zf.reveal 的事件。

如何在父元素(卡片组件)上侦听此事件,然后在调用时将 launchModal 更改为 false?

谢谢!

【问题讨论】:

  • 你能显示modal-component的Vue代码吗?
  • 这是两个组件的要点。我确定我在这方面做错了很多,这是我第一次在大型应用程序上使用 Vue,请温柔一点。哈哈!我知道很多 JS 都可以重构,我只是想让一切都先正常工作;)gist.github.com/DinsmoreDesign/64d2e45119e842838507c192283ff8c8 仅供参考:正在传递的数据来自父应用程序上的 API 调用,并通过道具传递。
  • 那是卡片,组件,我们能看到模态组件吗?
  • 它就在那里,你需要向下滚动一点;)
  • 德普。我看到了。我想这将是this.$el 然后在我的回答中。

标签: javascript vue.js zurb-foundation vuejs2 vue-component


【解决方案1】:

基本上这可能归结为,在您的 modal-component 中(将这些添加到 Modal.vue 中的脚本中)

methods:{
    onModalClosed(){
        this.$emit("modal-closed")
    }
},
mounted(){
    this.$el.addEventListener('closed.zf.reveal', this.onModalClosed)
},
beforeDestroy(){
    this.$el.removeEventListener('closed.zf.reve‌​al', this.onModalClosed)
}

或类似的效果,取决于发出事件的元素。如果某个其他元素发出了closed.zf.reveal 事件,那么您可以向它添加ref="modal",然后使用this.$refs.modal.addEventListener 和this.$refs.modal.removeEventListener。

那你就可以了

<modal-component v-if="launchModal === true"
                 @modal-closed="launchModal = false">
</modal-component>

编辑

所以监听事件的问题是 Foundation 使用 jQuery 来触发事件。这意味着您不能使用本机方法 (addEventListener) 来监听它,您必须使用 jQuery 来监听它。所以上面修改后的代码是这样的:

methods:{
    onModalClosed(){
        this.$emit("modal-closed")
    }
},
mounted(){
    $(this.$el).on('closed.zf.reveal', this.onModalClosed)
},
beforeDestroy(){
    $(this.$el).off('closed.zf.reve‌​al', this.onModalClosed)
}

事实上,这确实捕捉到了事件。问题是,无论出于何种原因,Foundation 将模态 移到 Vue 之外 并在模态初始化时将其附加到文档的底部。这会导致 Vue 在 launchModal 设置为 false 时抛出错误,因为模态不再在 Vue 内部,并且 Vue 在尝试将其从 DOM 中删除时会抱怨。

既然如此,我建议你使用v-if inside模态为things that are rendering very slowly。这将产生一个像这样的组件。

Vue.component("modal", {
  props:["show"],
  template: "#modal-template",
  watch:{
    show(newVal){
      if (newVal)
        $(this.$el).foundation("open")
    }
  },
  methods:{
    onModalClosed(){
      this.$emit("modal-closed")
    }
  },
  mounted() {
    new Foundation.Reveal($(this.$el))
    $(this.$el).on("closed.zf.reveal", this.onModalClosed);
  },
  beforeDestroy() {
    $(this.$el).off("closed.zf.reveal", this.onModalClosed);
  }
});

模板是

<template id="modal-template">
  <div class="reveal" data-reveal>
    <div v-if="show">
      Stuff that is expensive to render
    </div>
    <button class="close-button" data-close aria-label="Close modal" type="button">
      <span aria-hidden="true">&times;</span>
    </button>
  </div>
</template>

这是工作的example。

【讨论】:

  • 别忘了beforeDestroy(){this.$el.removeEventListener('closed.zf.reveal', () =&gt; this.$emit('modal-closed'))}
  • @BertEvans 谢谢!这对我来说很有意义,但由于某种原因仍然无法正常工作。我假设模态组件上的事件实际上应该是@modal-closed,正如组件的mounted/beforeDestroy 生命周期中所引用的那样,但这仍然没有出于某种原因将其切换为false。我想知道模态是否真的没有发出那个事件,毕竟?老实说,Foundation 6 的 JS 文档很糟糕。
  • @MatrixClaw 是的,监听器上的事件名称是错误的。我修好了。我会看一下文档。
  • @MatrixClaw 我现在明白了。加入我邀请你到的房间。
  • @RoyJ 似乎绑定。 github.com/vuejs/vue/blob/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-10-28
  • 1970-01-01
  • 2023-03-14
  • 2016-10-22
  • 1970-01-01
  • 1970-01-01
  • 2020-01-17
相关资源
最近更新 更多