简体   繁体   English

在Vue.js听取基金会活动?

[英]Listen for Foundation event in Vue.js?

I'm building an app in Vue.js with the general structure of: 我正在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>

This allow me to render the modal on the DOM only if a data element of launchModal is set to true (after clicking the button to launch the modal). 这允许我仅在launchModal的数据元素设置为true时(在单击按钮以启动模态之后)才在DOM上呈现模态。 This works great, but I need to do the reverse when it's closed. 这很好用,但是当它关​​闭时我需要反向。

According to Foundation's documentation, the Reveal (modal) component should emit an event called closed.zf.reveal when it's closed. 根据Foundation的文档,Reveal(模态)组件应该在关闭时发出一个名为closed.zf.reveal的事件。

How do I listen for this event on the parent element (card-component) and then change launchModal to false, when it's called? 如何在父元素(card-component)上侦听此事件,然后在调用时将launchModal更改为false?

Thanks! 谢谢!

Essentially this will likely boil down to, in your modal-component (add these to the script in Modal.vue) 基本上这可能归结为你的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)
}

Or something to that effect, depending on what element emits the event. 或者那种效果,取决于发出事件的元素。 If some other element emits the closed.zf.reveal event, then you could add a ref="modal" to it and then use this.$refs.modal.addEventListener and this.$refs.modal.removeEventListener . 如果某个其他元素发出closed.zf.reveal事件,那么你可以添加一个ref="modal"然后使用this.$refs.modal.addEventListenerthis.$refs.modal.removeEventListener

Then you could just 那你就可以了

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

Edit 编辑

So the issue with listening to the event is that Foundation is using jQuery to fire the event. 因此,监听事件的问题是Foundation正在使用jQuery来触发事件。 That means that you cannot listen for it using native methods ( addEventListener ), you have to listen to it with jQuery. 这意味着您无法使用本机方法( addEventListener )来监听它,您必须使用jQuery来监听它。 So the modified code from above would be this: 所以上面修改过的代码是这样的:

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

And this does, in fact, catch the event. 事实上,这确实可以吸引这一事件。 The problem is that Foundation, for whatever reason, moves the modal outside of the Vue and appends it to the bottom of the document when the modal is initialized. 问题在于,无论出于何种原因,基金会将模态移动到Vue 之外 ,并在模态初始化时将其附加到文档的底部。 That causes Vue to throw an error when launchModal is set to false because the modal is no longer inside the Vue, and Vue complains when it tries to remove it from the DOM. 这会导致Vue在launchModal设置为false时抛出错误,因为模式不再 Vue中,并且Vue在尝试将其从DOM中删除时会抱怨。

That being the case, I suggest you use your v-if inside the modal for the things that are rendering very slowly . 既然如此,我建议你用你的v-if该模式为内部 正在呈现非常缓慢的事情 That will result in a component like this. 这将导致像这样的组件。

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

And the template is 而模板是

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

And here is the working example . 这是工作的例子

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM