vue3新增特性

一、Vue 3 新增特性

1. Composition API
  • 概述
    • Composition API 提供了一种更灵活和强大的方式来组织和复用逻辑。
    • 适用于复杂组件和逻辑复用场景。
  • 主要功能
    • setup 函数:组件的入口点,用于定义响应式数据、方法、生命周期钩子等。
    • 响应式 APIrefreactive 提供更细粒度的响应式控制。
    • 生命周期钩子onMountedonUnmounted 等新生命周期钩子。
    • 计算属性和监听器computedwatch 的 Composition API 版本。
  • 示例
    <template>
      <div>
        <p>{
        
        { count }}</p>
        <button @click="increment">Increment</button>
      </div>
    </template>
    
    <script>
    import { ref, onMounted } from 'vue';
    
    export default {
      setup() {
        const count = ref(0);
    
        const increment = () => {
          count.value++;
        };
    
        onMounted(() => {
          console.log('Component mounted');
        });
    
        return { count, increment };
      }
    };
    </script>
    
2. Teleport
  • 概述
    • Teleport 允许将组件的内容渲染到 DOM 中的其他位置,而不改变组件的逻辑结构。
  • 用途
    • 适用于模态框、提示框等需要脱离当前组件树的场景。
  • 示例
    <template>
      <button @click="showModal = true">Open Modal</button>
      <teleport to="body">
        <div v-if="showModal" class="modal">
          <p>This is a modal!</p>
          <button @click="showModal = false">Close</button>
        </div>
      </teleport>
    </template>
    
    <script>
    import { ref } from 'vue';
    
    export default {
      setup() {
        const showModal = ref(false);
        return { showModal };
      }
    };
    </script>
    
    <style>
    .modal {
      position: fixed;
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%);
      background: white;
      pad
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

香蕉可乐荷包蛋

努力写有用的code

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值