• Vue3和Vue2的部分用法差异 (持续更新中)


    一、指令篇

    Vue3与Vue2之间生命周期函数在销毁的时候有变化

    Vue2 --> Vue3

    beforeDestroy --> beforeUnmount

    destroyed --> unmounted

    vue3中的setup() 等于vue2生命周期的beforeCreate + created

    this

    vue2中:一般情况下都要使用this

    vue3中:因为setup函数的存在,所有的props、data等都不需要用this进行访问(vue3对                     vue2绝大多数是兼容的,如果你用了vue2相关的东西,那你还是需要像vue2一样                   书写)

    定义全局变量的方法变化

    Vue2

    定义:
    Vue.prototype.$http = ""
    Vue.prototype.$baseUrl= "http://test"

    使用:

    页面里面用的时候直接 this.$baseUrl


    Vue3
    const app = createApp({})

    定义

    方式一:app.config.globalProperties.$baseUrl = "http://test"
    方式二:app.$temp = "$temp-全局变量"+window.location.origin

    使用:

    const { proxy,appContext } = getCurrentInstance();

    方式一取用:console.error("proxy--", proxy.$baseUrl);
    方式二取用:console.error("appContext--", appContext.app.$temp);

    创建Vue实例变化

    Vue2

    import Vue from "vue";
    
    new Vue({
      el: "#app",
      router,
      store,
      render: (h) => h(App),
    });

    Vue3

    import { createApp } from "vue";
    
    const app = createApp(App);
    app.use(Vant)
        .use(store)
        .use(router)
        .mount("#app");

    Vue3中v-model取代了.sync

    变更:在自定义组件上使用v-model时, 属性、事件的默认名称变了 ( modelValue->value )

    变更:v-bind的.sync修饰符在 Vue3 中被去掉了, 合并到了v-model里

    新增:同一组件可以同时设置多个 v-model

    新增:开发者可以自定义 v-model修饰符

    1. <input v-model="searchValue"><input>
    2. <input :value="searchValue" @input="searchValue=$event"><input>
    3. <input v-model="isVisible">input>
    4. <input :modelValue="isVisible" @update:modelValue="isVisible = $event">input>
    1. <son
    2. v-model:mobileNo="mobileNo"
    3. v-model:form="selectForm"
    4. :tableDataTest="tableDataTest"
    5. @sonClick="sonClickBy"
    6. />
    7. // 子组件emit传值给父组件
    8. const emit = defineEmits();
    9. // 同步修改父组件的值
    10. const changeValue = (value)=>{
    11. emit("update:mobileNo", value)
    12. }

    Vue3同时使用v-if 和 v-for时,v-if 比 v-for 优先级更高

    Vue2同时使用v-if 和 v-for时,v-for 比 v-if 优先级更高

    插槽使用变化

    1. <div>
    2. <slot :current="toolTipData" name="test">slot> // 具名 作用域插槽
    3. <slot>slot> //默认插槽
    4. div>
    5. <test>
    6. <template>
    7. <div>默认插槽div>
    8. template>
    9. // 作用域插槽
    10. <template slot="test" slot-scope="{ current }">
    11. <div>具名插槽div>
    12. template>
    13. test>
    14. <div>
    15. <slot name="test" :newData="slotsData">slot>
    16. <slot>slot>
    17. div>
    18. <test>
    19. <template #default> // 可以写为v-slot:default #后面跟的是插槽名称
    20. <div>默认插槽div>
    21. template>
    22. //作用域插槽
    23. <template #test="{ newData }"> // 可以写为v-slot:test="newData"
    24. <p>{{ newData.aa }}p>
    25. <p>{{ newData.bb }}p>
    26. template>
    27. test>

    Vue.prototype 替换为 config.globalProperties

    vue2中:绑定全局的变量、方法等:Vue.prototype.$ajax = xxxx
    vue3中:const app = createApp({});

                   app.config.globalProperties.$ajax = xxxx

  • 相关阅读:
    lua profile 性能分析工具都有哪些
    HDFS总结
    MySQL中的锁
    系统设计.秒杀系统
    数据结构(1)线性结构——数组、链表、堆栈、队列(介绍和JAVA代码实现)
    pytorch深度学习实战lesson11
    《异 步》
    105. 从前序与中序遍历序列构造二叉树
    IEEE出版社旗下期刊的投稿整理——(更新ing)
    Java & 计算机编码
  • 原文地址:https://blog.csdn.net/hzj1369/article/details/126966682