Vue.js从子组件关闭模式

Vue.js从子组件关闭模式,vue.js,vuejs2,vue-component,Vue.js,Vuejs2,Vue Component,我想从此模式中的子组件关闭vuejs模式。 案例: 在SomeComponent中,我想关闭一些模态。 这是个好办法吗?可以用更好的方法吗?请再会, 关于当然没问题。您可以使用Vue的自定义事件系统: 至于判断这是否是理想的方法,没有人能用您提供的最少信息来判断。您需要使用this从子组件发出事件。$emit('exit',true) 然后在父组件(模态)中侦听该事件 然后将逻辑写入closeModal函数。这里是我自己制作的一个自定义模式,它使用slot将内容推入其中 myModal

我想从此模式中的子组件关闭vuejs模式。 案例:


在SomeComponent中,我想关闭一些模态。 这是个好办法吗?可以用更好的方法吗?请再会,
关于

当然没问题。您可以使用Vue的自定义事件系统:


至于判断这是否是理想的方法,没有人能用您提供的最少信息来判断。

您需要使用
this从子组件发出事件。$emit('exit',true)

然后在父组件(模态)中侦听该事件



然后将逻辑写入closeModal函数。

这里是我自己制作的一个自定义模式,它使用slot将内容推入其中

myModal.vue


<template>
    <transition name="modal">
        <div class="modal-mask">
            <div class="modal-wrapper">
                <header class="modal-header header-toolbar">
                    <h3>
                        <span>{{modalTitle}}</span>
                    </h3>
                    <span class="icon">
                        <i class="fa fa-times" aria-hidden="true" @click="hideModal"></i>
                    </span>
                </header>
                <section class="modal-body">
                    <slot></slot>
                </section>
            </div>
        </div>
    </transition>
</template>

<script>
    export default {
        name: 'my-modal',
        props: {
            modalTitle: {
                type: String,
                default: null,
            },
        },
        methods: {
            hideModal() {
                this.$emit('hide');
            },
        },
    };
</script>
myModal.vue
{{modalTitle}}
导出默认值{
名称:'我的模态',
道具:{
模式:{
类型:字符串,
默认值:null,
},
},
方法:{
hideModal(){
这个。$emit('hide');
},
},
};
如何使用它:

<my-modal v-if="modalState" @hide="modalState = false">
            ...Content
</my-modal>

…内容
将数据中的modalState设置为false,或将其定义为prop或其他类型。
当你想显示模态时,把它改为true。如果您需要类定义,我也可以给您SCS

您可以使用作用域插槽传递回调以关闭父模式,例如:

modal.vue

<template>
  <div>
    <slot :close="close">
    </slot>
  </div>
</template>

<script>
  export default {
    name: 'Modal',
    methods: {
      close() {
        // Whatever you do to close the modal
      }
    }
  }
</script>
modal.vue
导出默认值{
名称:'模态',
方法:{
关闭(){
//无论你做什么来关闭模态
}
}
}
现在,您可以在插槽中使用它:

<modal name="some modal" v-slot="slotScope">
  <some-component @close="slotScope.close" />
</modal>

这样,每当您在“某个组件”中发出“close”事件时,它就会触发“modal”组件中的close方法

modal.vue

<template>
  <div>
    <slot :close="close">
    </slot>
  </div>
</template>

<script>
  export default {
    name: 'Modal',
    methods: {
      close() {
        // Whatever you do to close the modal
      }
    }
  }
</script>
<modal name="some modal" v-slot="slotScope">
  <some-component @close="slotScope.close" />
</modal>