初始Vue3.0(12)——瞬间移动 第二部分

时间:2021-02-28 21:52:42   收藏:0   阅读:28

Teleport - 瞬间移动 第二部分

Modal 组件

<template>
<teleport to="#modal">
  <div id="center" v-if="isOpen">
    <h2><slot>this is a modal</slot></h2>
    <button @click="buttonClick">Close</button>
  </div>
</teleport>
</template>
<script lang="ts">
import { defineComponent } from ‘vue‘
export default defineComponent({
  props: {
    isOpen: Boolean,
  },
  emits: {
    ‘close-modal‘: null
  },
  setup(props, context) {
    const buttonClick = () => {
      context.emit(‘close-modal‘)
    }
    return {
      buttonClick
    }
  }
})
</script>
<style>
  #center {
    width: 200px;
    height: 200px;
    border: 2px solid black;
    background: white;
    position: fixed;
    left: 50%;
    top: 50%;
    margin-left: -100px;
    margin-top: -100px;
  }
</style>

在 App 组件中使用

const modalIsOpen = ref(false)
const openModal = () => {
  modalIsOpen.value = true
}
const onModalClose = () => {
  modalIsOpen.value = false
}

<button @click="openModal">Open Modal</button><br/>
<modal :isOpen="modalIsOpen" @close-modal="onModalClose"> My Modal !!!!</modal>

原文:https://www.cnblogs.com/duet/p/14460109.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!