Vue3学习笔记---新的组件

新的组件

Fragment

----片段
  • 在Vue2中:组件必须要有一个根标签
  • 在Vue3中:组件可以没有根标签,内部会将多个标签包含在一个Fragment虚拟元素中
  • 好处:减少标签层级,较少内存占用

Teleport

----瞬间移动,传送
  • 什么事Teleport?----Teleport是一种能够将我们的组件html结构移动到指定位置的技术

    <template>
      <div>
        <button @click="isShow = true">点我弹窗</button>
        <!-- 把弹窗从藏得很深很深的组件里面传送到任意想传送的组件中,相当于可以指定该弹窗向某个固定的HTML元素定位,不会受到别的定位元素的影响 -->
        <teleport to="body">
          <div class="mask" v-if="isShow">
            <div class="dialog">
              <h3>我是一个弹窗</h3>
              <h4>内容1</h4>
              <h4>内容1</h4>
              <h4>内容1</h4>
              <h4>内容1</h4>
              <button @click="isShow = false">关闭弹窗</button>
            </div>
          </div>
        </teleport>
      </div>
    </template>
    
    <script>
    import { ref } from "vue";
    export default {
      name: "dialog",
      setup() {
        let isShow = ref(false);
        return { isShow };
      },
    };
    </script>
    
    <style>
    .mask {
      position: absolute;
      top: 0;
      bottom: 0;
      left: 0;
      right: 0;
      background-color: rgba(0, 0, 0, 0.5);
    }
    .dialog {
      position: absolute;
      /* 50%参考的是定位的父元素 */
      top: 50%;
      left: 50%;
      /* -50%参考的是自身 */
      transform: translate(-50%, -50%);
      text-align: center;
      width: 300px;
      height: 300px;
      background-color: green;
    }
    </style>

评论

h
hw_7864

⭐️ Admin快速开发: https://show.cool-admin.com/

赞 0
h
hw_7864

⭐️ 官方文档: https://cool-js.com

赞 0
h
hw_7864

⭐️ chatgpt在线体验: https://cool-js.com/ai/chat-vip/index.html?r=helloworld

赞 0