Vue3新增特性 之 全局config.globalProperties和Teleport内置组件
    		       		warning:
    		            这篇文章距离上次修改已过438天,其中的内容可能已经有所变动。
    		        
        		                
                
<template>
  <div>
    <!-- 使用Teleport传送内容到body下 -->
    <teleport to="body">
      <div v-if="isOpen" class="modal">
        Modal Content
        <button @click="isOpen = false">Close</button>
      </div>
    </teleport>
 
    <!-- 使用globalProperties定义的方法 -->
    <button @click="globalMethod">Call Global Method</button>
  </div>
</template>
 
<script>
export default {
  data() {
    return {
      isOpen: false,
    };
  },
  methods: {
    globalMethod() {
      // 调用全局属性定义的方法
      this.$myGlobalMethod();
    }
  },
  mounted() {
    // 在组件挂载后打开模态框
    this.isOpen = true;
  }
};
</script>
 
<style>
.modal {
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  background-color: white;
  padding: 20px;
  border-radius: 8px;
  z-index: 10;
}
</style>这个简单的Vue 3示例展示了如何使用Teleport内置组件将模态框的内容传送到body标签下,以及如何使用全局配置globalProperties定义的方法。在mounted钩子中,模态框被设置为在组件加载后显示。
评论已关闭