首页 > 移动平台 > 详细

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

时间:2021-02-28 21:52:42      阅读:27      评论:0      收藏:0      [点我收藏+]

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>

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

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!