• Vue3(笔记)


    Vue3项目的创建

    使用vue-cli创建

    PS D:\web\vue3> vue --version
    @vue/cli 5.0.6
    PS D:\web\vue3> vue create vue3_test
    
    • 1
    • 2
    • 3

    使用vite创建

    什么是vite? - 新一代前端构建工具

    • 开发环境中,无需打包操作,可快速的冷启动
    • 轻量快速的热重载(HMR)
    • 真正的按需编译,不再等待整个应用编译完成

    传统构建与vite构建对比图
    在这里插入图片描述
    创建步骤

    ## 创建工程
    npm init vite-app <project-name>
    ## 进入工程目录
    cd <project-name>
    ## 安装依赖
    npm install
    ## 运行
    npm run dev
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    具体应用

    PS D:\web\vue3> npm init vite-app vue3_test_vite
    Need to install the following packages:
      create-vite-app@1.21.0
    Ok to proceed? (y) y
    npm WARN deprecated create-vite-app@1.21.0: create-vite-app has been deprecated. run `npm init @vitejs/app` or `yarn create @vitejs/app` instead.
    Scaffolding project in D:\web\vue3\vue3_test_vite...
    
    Done. Now run:
    
      cd vue3_test_vite
      npm install (or `yarn`)
      npm run dev (or `yarn dev`)
    
    PS D:\web\vue3>
    PS D:\web\vue3> cd .\vue3_test_vite\
    PS D:\web\vue3\vue3_test_vite> npm install
    
    added 290 packages in 18s
    PS D:\web\vue3\vue3_test_vite> npm run dev
    
    > vue3_test_vite@0.0.0 dev
    > vite
    
    [vite] Optimizable dependencies detected:
    vue
    
      Dev server running at:
      > Network:  http://10.33.201.152:3000/
      > Local:    http://localhost:3000/
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29

    常用 Composition API

    官方文档

    拉开序幕的setup()

    1. 理解:Vue3.0中一个新的配置项,值为一个函数。
    2. 组件中所用到的:数据、方法等等,均要配置在setup中。
    3. setup函数的两种返回值:① 若返回一个对象,则对象中的属性、方法, 在模板中均可以直接使用。(重点关注!) ② 若返回一个渲染函数:则可以自定义渲染内容。(了解)
    4. 注意点:

    1、尽量不要与Vue2.x配置混用
    - Vue2.x配置(data、methods、computed…)中可以访问到setup中的属性、方法
    - 但在setup中不能访问到Vue2.x配置(data、methods、computed…)
    - 如果有重名,setup优先
    2、setup不能是一个async函数,因为返回值不再是return的对象,而是promise,模板看不到return对象中的属性,后期也可以返回一个Promise实例,但需要Suspense和异步组件的配合

    <template>
      <h1>一个人的信息</h1>
      <h2>姓名:{{name}}</h2>
      <h2>年龄:{{age}}</h2>
      <button @click="sayHello">说话</button>
    </template>
    
    <script>
    import {h} from 'vue'
    export default {
      name: "App",
      setup() {
        // 数据 定义了数据直接使用即可
        let name = '张三'
        let age = 18
    
        // 方法
        function sayHello() {
          alert(`我叫${name},我今年${age}岁了,你好啊!`)
        }
    
        /* 
          setup函数的两种返回值
            - 1、若返回一个对象,则对象中的属性、方法、在模板中均可以直接使用(重点关注)
            - 2、若返回一个渲染函数,则可以自定义渲染内容(了解)
        */
        // 1 返回一个对象(常用)
        /* return {
          name, 
          age, 
          sayHello
        } */
    
        // 2 返回一个渲染函数(了解)
        // return () => {h('h1', '尚硅谷')}
        return () => h('h1', '尚硅谷')
    
        /* 
          注意点:
          1、尽量不要与Vue2.x配置混用
            - Vue2.x配置(data、methods、computed...)中可以访问到setup中的属性、方法
            - 但在setup中不能访问到Vue2.x配置(data、methods、computed...)
            - 如果有重名,setup优先
          2、setup不能是一个async函数,因为返回值不再是return的对象,而是promise,模板看不到return对象中的属性
        */
    
      }
    };
    </script>
    
    <style>
    #app {
      font-family: Avenir, Helvetica, Arial, sans-serif;
      -webkit-font-smoothing: antialiased;
      -moz-osx-font-smoothing: grayscale;
      text-align: center;
      color: #2c3e50;
      margin-top: 60px;
    }
    </style>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60

    ref函数

    • 作用:定义一个响应式的数据
    • 语法
    const xxx = ref(initValue)
    
    • 1

    ① 创建一个包含响应式数据的引用对象(reference对象,简称ref对象)
    ② JS中操作数据:xxx.value
    ③ 模板中读取数据:不需要.value,直接

    <div>{{xxx}}</div>
    
    • 1

    备注:

    • 接收的数据可以是:基本类型、也可以是对象类型。
    • 基本类型的数据:响应式依然是靠Object.defineProperty()getset完成的。
    • 对象类型的数据:内部 “ 求助 ” 了Vue3.0中的一个新函数—— reactive函数。

    ref函数_处理基本类型 && ref函数_处理对象类型

    <template>
      <h1>一个人的信息</h1>
      <h2>姓名:{{ name }}</h2>
      <h2>年龄:{{ age }}</h2>
      <h2>工作种类:{{ job.type }}</h2>
      <h2>工作薪水:{{ job.salary }}</h2>
      <button @click="changeInfo">更新修改人的信息</button>
    </template>
    
    <script>
    import { ref } from "vue";
    export default {
      name: "App",
      setup() {
        // 数据
        let name = ref("张三");
        let age = ref(18);
        let job = ref({
          type: "前端工程师",
          salary: "30k",
        });
    
        // 方法
        function changeInfo() {
          name.value = "李四";
          age.value = 48;
          // console.log(name, age);
    
          // console.log(job.value)
          job.value.type = "UI设计师";
          job.value.salary = "60k";
        }
    
        return {
          name,
          age,
          job,
          changeInfo,
        };
      },
    };
    </script>
    
    <style>
    #app {
      font-family: Avenir, Helvetica, Arial, sans-serif;
      -webkit-font-smoothing: antialiased;
      -moz-osx-font-smoothing: grayscale;
      text-align: center;
      color: #2c3e50;
      margin-top: 60px;
    }
    </style>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53

    reactive函数

    • 作用: 定义一个对象类型的响应式数据(基本类型不要用它,要用ref函数)
    • 语法:const 代理对象= reactive(源对象)接收一个对象(或数组),返回一个代理对象(Proxy的实例对象,简称proxy对象)
    • reactive定义的响应式数据是“深层次的”。
    • 内部基于 ES6 的 Proxy 实现,通过代理对象操作源对象内部数据进行操作
    <template>
      <h1>一个人的信息</h1>
      <h2>姓名:{{ person.name }}</h2>
      <h2>年龄:{{ person.age }}</h2>
      <h2>工作种类:{{ person.job.type }}</h2>
      <h2>工作薪水:{{ person.job.salary }}</h2>
      <h2>爱好{{ person.hobby }}</h2>
      <h2>测试的数据c:{{ person.job.a.b.c }}</h2>
      <button @click="changeInfo">更新修改人的信息</button>
    </template>
    
    <script>
    import { reactive } from "vue";
    export default {
      name: "App",
      setup() {
        // 我们在书写的时候有一个套路 - 我们一般将组件所用到的数据封装到一个对象当中
        /* let preson = {
          name: "张三",
          age: 18,
          job: {
            type: "前端工程师",
            salary: "30k",
            a: {
              b: {
                c: 666,
              },
            },
          },
          hobby:['抽烟', '喝酒', '烫头']
        };
    
        let p = reactive(preson) */
    
        let person = reactive({
          name: "张三",
          age: 18,
          job: {
            type: "前端工程师",
            salary: "30k",
            a: {
              b: {
                c: 666,
              },
            },
          },
          hobby: ["抽烟", "喝酒", "烫头"],
        });
    
        // 方法
        function changeInfo() {
          person.name = "李四";
          person.age = 48;
          person.job.type = "UI设计师";
          person.job.salary = "60k";
          person.job.a.b.c = 999;
          person.hobby[0] = "学习";
        }
    
        return {
          person,
          changeInfo,
        };
      },
    };
    </script>
    
    <style>
    #app {
      font-family: Avenir, Helvetica, Arial, sans-serif;
      -webkit-font-smoothing: antialiased;
      -moz-osx-font-smoothing: grayscale;
      text-align: center;
      color: #2c3e50;
      margin-top: 60px;
    }
    </style>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77

    上面提到的:基本类型不要用它,要用ref函数

    • 但是使用ref函数的话,在获取属性值的时候,经常使用到value
    • 我们可以用reactive来进行**,我们可以将属性值加到对象中**,并使用reactive函数,上面都有体现!!

    回顾Vue2的响应式原理

    实现原理

    • 对象类型:通过Object.defineProperty()对属性的读取、修改进行拦截(数据劫持)
    • 数组类型:通过重写更新数组的一系列方法来实现拦截。(对数组的变更方法进行了包裹)。
    Object.defineProperty(data, 'count', {
        get () {}, 
        set () {}
    })
    
    • 1
    • 2
    • 3
    • 4

    存在问题

    • 新增属性、删除属性, 界面不会更新。
    • 直接通过下标修改数组, 界面不会自动更新。

    通过demo来演示vue存在的问题,在App组件中进行演示

    <template>
      <div>
        <h1>我是Vue2写的效果</h1>
        <h2 v-show="person.name">姓名:{{ person.name }}</h2>
        <h2>年龄:{{ person.age }}</h2>
        <h2 v-show="person.sex">性别:{{ person.sex }}</h2>
        <h2>我的爱好是:{{ person.hobby }}</h2>
        <button @click="addSex">添加一个sex属性</button>
        <button @click="deleteName">删除name属性</button>
        <button @click="updateHobby">修改第一个爱好</button>
      </div>
    </template>
    
    <script>
    import Vue from "vue";
    export default {
      name: "App",
      data() {
        return {
          person: {
            name: "张三",
            age: 18,
            hobby: ["学习", "爱好"],
          },
        };
      },
      methods: {
        addSex() {
          // console.log("添加前", this.person.sex); // undefined
          // this.person.sex = "女";
          // console.log("添加后", this.person.sex); // 女 属性值是修改了,但是页面没有呈现出来
          // 在vue2中也可以修复这种问题,使用this.$set() 来解决
          this.$set(this.person, "sex", "女"); // 在Vue3项目中直接书写是不行的
          // Vue.set(this.person, "sex", "女"); // 在Vue3项目中直接书写是不行的
        },
        deleteName() {
          // console.log(this.person.name);
          // delete this.person.name;
          // console.log(this.person.name);
          // 上面也是删除了,但是在页面中没有呈现出来
          // 也有相应的解决办法
          // this.$delete(this.person, 'name')
          Vue.delete(this.person, "name");
        },
        updateHobby() {
          // this.person.hobby[0] = "抽烟"; 
          // console.log(this.person.hobby) 确实修改了,但是页面不进行展示
          // this.$set(this.person.hobby, 0, '逛街') 可以
          // 调用数组身上的变更方法, 
          this.person.hobby.splice(0, 1, '逛街')
        },
      },
    };
    </script>
    
    <style>
    #app {
      font-family: Avenir, Helvetica, Arial, sans-serif;
      -webkit-font-smoothing: antialiased;
      -moz-osx-font-smoothing: grayscale;
      text-align: center;
      color: #2c3e50;
      margin-top: 60px;
    }
    </style>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65

    注意

    • 在Vue3项目中使用this.$set 和 Vue.set会出现报错,需要在vue2中使用
    • 数组中splice() 来插入操作的使用复习,:**前两个参数指定要删除哪些元素,**这两个参数后面可以跟任意多个参数,表示要在第一个参数指定的位置插入到数组中的元素,例如:
    let a = [1, 2, 3, 4, 5]
    a.splice(2, 0, 'a', 'b') // [], a现在是[1, 2, 'a', 'b', 3, 4, 5]
    a.splice(2, 2, [1, 2], 3) // ['a', 'b'], a现在是[1, 2, [1, 2], 3, 3, 4, 5]
    
    • 1
    • 2
    • 3

    模拟Vue2的响应式原理

    <script>
        // 源数据
        let person = {
            name: '张三',
            age: 18
        }
    
        // 模拟Vue2中响应式原理
        /* let p = {}
        Object.defineProperty(p, 'name', {
            configurable:true, // 配置为true表示是可删除的
            get() { // 有人读取name时调用
                return person.name
            }, 
            set(value) { // 有人修改name时调用
                console.log('有人修改了name属性,我发现了,我要去更新界面!')
                person.name = value
            }
        })
    
        Object.defineProperty(p, 'age', {
            get() { // 有人读取age时调用
                return person.age
            }, 
            set(value) { // 有人修改age时调用
                console.log('有人修改了age属性,我发现了,我要去更新界面!')
                person.age = value
            }
        }) */
    </script>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30

    Vue3的响应式原理

    • 模拟Vue3中实现响应式 window.Proxy
    • 使用Proxy代理
    • 想要实现响应式,必须捕获到数据的修改操作

    使用Vue3来实现响应式的原理: - 很好的解决了Vue2遗留下来的问题

    模拟Vue3的响应式原理

    • 通过Proxy(代理): 拦截对象中任意属性的变化, 包括:属性值的读写、属性的添加、属性的删除等。
    • 通过Reflect(反射): 对源对象的属性进行操作。
    <script>
        // 源数据
        let person = {
            name: '张三',
            age: 18
        }
        // 模拟Vue3中实现响应式 window.Proxy
        // Proxy 代理
        // 真想实现响应式,必须捕获到数据的修改操作
        /* 
            person - 源数据
            p - 代理数据
        */
        const p = new Proxy(person, {
            // 有人读取p的某个属性时调用
            get(target, propName) {
                // console.log('有人读取了p身上的某个属性', target, propName)
                console.log(`有人读取了p身上的${propName}属性`)
                return target[propName]
            },
            // 有人修改p的某个属性时或给p追加某个属性时调用
            set(target, propName, value) {
                // console.log(target, propName, value)
                console.log(`有人修改了p身上的${propName}属性,我要去更新界面了`)
                target[propName] = value
            },
            // 有人删除p的某个属性时调用
            deleteProperty(target, propName) {
                console.log(`有人删除了p身上的${propName}属性,我要去更新界面了`)
                return delete target[propName]
            }
        })
    </script>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33

    reactive对比ref

    从定义数据的角度对比

    • ref用来定义:基本类型数据
    • reactive用来定义:对象(或数组)类型数据
    • 备注:ref也可以用来定义对象(或数组)类型数据, 它内部会自动通过reactive转为代理对象

    从原理的角度对比

    • ref通过Object.defineProperty()getset来实现响应式(数据劫持)。
    • reactive通过使用Proxy来实现响应式(数据劫持), 并通过Reflect操作源对象内部的数据。

    从使用角度对比

    • ref定义的数据:操作数据需要.value,读取数据时模板中直接读取不需要.value
    • reactive定义的数据:操作数据与读取数据:均不需要.value

    当我们所需要配置的数据有很多种的时候,我们可以封装到一个对象中,如果有很多不同类型的数据对象,我们可以在对象里面接着进行封装不同的对象,然后将数据data返回即可

    let data = reactive({
    	person:{},
    	student:{}
    })
    return {
    	data
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    setup的两个注意点

    setup执行的时机

    • 在beforeCreate之前执行一次,this是undefined。

    setup的参数

    • props:值为对象,包含:组件外部传递过来,且组件内部声明接收了的属性。
    • context:上下文对象

    ① attrs: 值为对象,包含:组件外部传递过来,但没有在props配置中声明的属性, 相当于 this.$attrs
    ② slots: 收到的插槽内容, 相当于 this.$slots
    ③ emit: 分发自定义事件的函数, 相当于 this.$emit

    父组件

    
    
    
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30

    子组件

    
    
    
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38

    computed计算属性

    • vue3中的计算属性,作为一个方法形式进行相应的引入即可
    • 与Vue2.x中computed配置功能一致

    简写形式

    person.fullName = computed(() => {
          return person.firstName + "-" + person.lastName;
        });
    
    • 1
    • 2
    • 3

    完整版写法 - 里面有相应的set以及get方法

    person.fullName = computed({
            get() {
                return person.firstName + "-" + person.lastName;
            }, 
            set(value){
                const nameArr = value.split('-')
                person.firstName = nameArr[0]
                person.lastName = nameArr[1]
            }
        });
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 计算属性是引入使用的,主要用于计算程序中的数据,
    
    
    
    
     
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56

    watch

    • 与Vue2.x中watch配置功能一致

    两个小坑

    • 监视reactive定义的响应式数据时:oldValue无法正确获取、强制开启了深度监视(deep配置失效)
    • 监视reactive定义的响应式数据中某个属性时:deep配置有效

    回顾Vue2中的watch的使用

    watch: {
        // 简写形式
        /* sum(newValue, oldValue) {
          console.log("sum的值变化了");
        }, */
        // 完整版写法 可以配置一些属性
        /* sum: {
          immediate: true, // 一上来就进行监听一次
          deep: true, // 深度监视 数据的层级是很多层的 因为默认是开启一个浅层次的监视
          handler(newValue, oldValue) {
            console.log("sum的值变化了", newValue, oldValue);
          },
        }, */
      },
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    watch进行监视的几种情况

    1、情况一:监视ref定义的响应式数据

      watch(sum,(newValue,oldValue)=>{
      	console.log('sum变化了',newValue,oldValue)
      },{immediate:true})
    
    • 1
    • 2
    • 3

    2、情况二:监视多个ref定义的响应式数据

     watch([sum,msg],(newValue,oldValue)=>{
     	console.log('sum或msg变化了',newValue,oldValue)
     }) 
    
    • 1
    • 2
    • 3

    3、情况三:监视reactive定义的响应式数据

    • 若watch监视的是reactive定义的响应式数据,则无法正确获得oldValue!!
    • 若watch监视的是reactive定义的响应式数据,则强制开启了深度监视
    watch(person,(newValue,oldValue)=>{
    	console.log('person变化了',newValue,oldValue)
    },{immediate:true,deep:false}) //此处的deep配置不再奏效
    
    • 1
    • 2
    • 3

    4、情况四:监视reactive定义的响应式数据中的某个属性

    watch(()=>person.age,(newValue,oldValue)=>{
    	console.log('person的job变化了',newValue,oldValue)
    },{immediate:true,deep:true}) 
    
    • 1
    • 2
    • 3

    5、情况五:监视reactive定义的响应式数据中的某些属性

    watch([()=>person.name,()=>person.age],(newValue,oldValue)=>{
    	console.log('person的job变化了',newValue,oldValue)
    },{immediate:true,deep:true})
    
    • 1
    • 2
    • 3

    6、特殊情况

    watch(()=>person.job,(newValue,oldValue)=>{
        console.log('person的job变化了',newValue,oldValue)
    },{deep:true}) //此处由于监视的是reactive素定义的对象中的某个属性,所以deep配置有效
    
    • 1
    • 2
    • 3

    总DEMO

    
    
    
    
     
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96

    ref定义的数据,在进行监测的时候,不能进行.value 操作,watch监测的不是一个具体的值,而应该是一个结构
    在这里插入图片描述
    在这里插入图片描述

    • sum里面存的是基本类型的值,不能.value
    • ref定义的对象属性,里面的值是多层次的,监视的时候,要么使用person.value 要么配置相应的deep:true 属性

    Vue3的生命周期

    声明周期图

    在这里插入图片描述
    注意点

    • Vue3.0中可以继续使用Vue2.x中的生命周期钩子,但有有两个被更名:
    • beforeDestroy改名为 beforeUnmount
    • destroyed改名为 unmounted
    • Vue3.0也提供了 Composition API 形式的生命周期钩子,与Vue2.x中钩子对应关系如下:
    • beforeCreate===>setup()
    • created=======>setup()
    • beforeMount ===>onBeforeMount
    • mounted=======>onMounted
    • beforeUpdate===>onBeforeUpdate
    • updated =======>onUpdated
    • beforeUnmount ==>onBeforeUnmount
    • unmounted =====>onUnmounted
    • 无论在哪里,setup() 里面都是最先执行的
      父组件
    <template>
      <button @click="isShowDemo = !isShowDemo">切换隐藏/显示</button>
      <Demo v-if="isShowDemo"></Demo>
    </template>
    
    <script>
    import { ref } from "vue";
    import Demo from "./components/Demo.vue";
    export default {
      name: "App",
      components: { Demo },
      setup() {
        let isShowDemo = ref(true);
        return {
          isShowDemo,
        };
      },
    };
    </script>
    
    <style>
    </style>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    子组件

    <template>
      <h1>当前求和为:{{ sum }}</h1>
      <button @click="sum++">点我+1</button>
    </template>
    
    <script>
    import {
      ref,
      onBeforeMount,
      onMounted,
      onBeforeUpdate,
      onUpdated,
      onBeforeUnmount,
      onUnmounted,
    } from "vue";
    export default {
      name: "Demo",
      setup() {
        console.log('setup')
        // 数据
        let sum = ref(0);
    
        onBeforeMount(() => {
          console.log('onBeforeMount')
        })
        // 通过组合式API的形式去使用生命周期钩子
    
        // 返回一个对象(常用)
        return {
          sum,
        };
      },
      // 通过配置项的形式使用声明周期钩子
      // #region
      /* beforeCreate() {
        console.log("---beforeCreate---");
      },
      created() {
        console.log("---created---");
      },
      beforeMount() {
        console.log("beforeMount");
      },
      mounted() {
        console.log("mounted");
      },
      beforeUpdate() {
        console.log("beforeUpdate");
      },
      updated() {
        console.log("updated");
      },
      beforeUnmount() {
        console.log("beforeUnmount");
      },
      unmounted() {
        console.log("unmounted");
      }, */
      // #endregion
    };
    </script>
    
    <style>
    </style> 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64

    Vue3中书写生命周期钩子有两种形式

    1. 以配置项的形式使用生命周期钩子
    2. 以组合API的形式使用生命周期钩子
      上面两种书写形式,在代码中都有所体现!

    自定义hook函数

    • 什么是hook?—— 本质是一个函数,把setup函数中使用的Composition API进行了封装。
    • 类似于vue2.x中的mixin。
    • 自定义hook的优势: 复用代码, 让setup中的逻辑更清楚易懂。

    想要复用的代码书写在hook文件夹中
    比如实现一个鼠标打点,显示出鼠标具体位置的方法,书写的方法要暴露出去 里面可以书写相应的函数方法

    import { reactive, onMounted, onBeforeUnmount } from "vue";
    export default function() {
        // 实现鼠标打点相关的数据
        let point = reactive({
            x: 0,
            y: 0,
        })
    
        // 实现鼠标打点相关的方法
        function savePoint(event) {
            point.x = event.pageX;
            point.y = event.pageY;
        }
    
        // 只要demo组件一挂载的时候,就立刻执行
        // 实现鼠标打点的相关方法
        onMounted(() => {
            window.addEventListener("click", savePoint);
        })
    
        onBeforeUnmount(() => {
            window.removeEventListener("click", savePoint);
        })
        return point
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    在组件中调用刚才在hooks中定义的方法

    <template>
      <h1>当前求和为:{{ sum }}</h1>
      <button @click="sum++">点我+1</button>
    
      <hr />
      <h2>当前点击时鼠标的坐标为:x:{{ point.x }}, y:{{ point.y }}</h2>
    </template>
    
    <script>
    import { ref } from "vue";
    import usePoint from '../hooks/usePoint'
    export default {
      name: "Demo",
      setup() {
        // 数据
        let sum = ref(0);
        let point = usePoint() 
       
        return {
          sum,
          point
        };
      },
    };
    </script>
    
    <style>
    </style> 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    toRef与toRefs

    • 作用:创建一个 ref 对象,其value值指向另一个对象中的某个属性。
    • 语法:const name = toRef(person,'name')
    • 应用: 要将响应式对象中的某个属性单独提供给外部使用时。
    • 扩展:toRefstoRef功能一致,但可以批量创建多个 ref 对象,语法:toRefs(person)

    组件示例

    <template>
      <h2>姓名:{{ name }}</h2>
      <h2>年龄:{{ age }}</h2>
      <h2>薪资:{{ job.j1.salary }}K</h2>
      <button @click="name += '~'">修改姓名</button>
      <button @click="age++">修改年龄</button>
      <!-- <button @click="salary++">修改薪资</button> -->
      <button @click="job.j1.salary++">修改薪资</button>
    </template>
    
    <script>
    import { reactive, toRef, toRefs } from "vue";
    export default {
      name: "Demo",
      setup() {
        // 切记setup() 里面的箭头函数是undefined
        // 数据
        let person = reactive({
          name: "张三",
          age: 18,
          job: {
            j1: {
              salary: 20,
            },
          },
        });
    
        // const name1 = person.name
        // console.log('%%%', name1)
    
        // const name2 = toRef(person, 'name') // ObjectRefImpl 中间人ref可以做响应式
        // console.log('####', name2)
    
        const x = toRefs(person)
        console.log('****', x)
    
        return {
          person, 
          /*
            toRef是引用一个  有引用关系,这一点很关键! 
            ref是复制一个
          */
          // name:toRef(person, 'name'), 
          // age:toRef(person, 'age'), 
          // // 第一个参数传入一个对象即可
          // salary:toRef(person.job.j1, 'salary')
    
          ...toRefs(person) // ...toRefs只是把第一层拆了出来作为一个对象
        };
      },
    };
    </script>
    
    <style>
    </style> 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • toRefs只是将第一层的数据拆出来,对象里面嵌套的内容,如果需要获取需要一层一层获取得到才行

    其他 Composition API

    shallowReactive 与 shallowRef

    • shallowReactive:只处理对象最外层属性的响应式(浅响应式)。
    • shallowRef:只处理基本数据类型的响应式, 不进行对象的响应式处理。

    什么时候使用 ?

    • 如果有一个对象数据,结构比较深, 但变化时只是外层属性变化 ===> shallowReactive。
    • 如果有一个对象数据,后续功能不会修改该对象中的属性,而是生新的对象来替换 ===> shallowRef。

    组件示例

    <template>
      <h4>当前的x.y的值是:{{ x.y }}</h4>
      <!-- <button @click="x.y++">点我x+1</button> -->
      <button @click="x={y:888}">点我替换</button>
      <hr />
      <h2>{{ person }}</h2>
      <h2>姓名:{{ name }}</h2>
      <h2>年龄:{{ age }}</h2>
      <h2>薪资:{{ job.j1.salary }}K</h2>
      <button @click="name += '~'">修改姓名</button>
      <button @click="age++">修改年龄</button>
      <!-- <button @click="salary++">修改薪资</button> -->
      <button @click="job.j1.salary++">修改薪资</button>
    </template>
    
    <script>
    import { ref, reactive, toRef, toRefs, shallowReactive, shallowRef } from "vue";
    export default {
      name: "Demo",
      setup() {
        // 切记setup() 里面的箭头函数是undefined
        // 数据
        // shallowReactive只考虑第一层数据的响应式
        let person = shallowReactive({
          name: "张三",
          age: 18,
          job: {
            j1: {
              salary: 20,
            },
          },
        });
    
        // let x = ref(0);
        // 第一层肯定是响应式的 就是看里面的,你会不会进行深挖的操作
        let x = shallowRef({
          y:0
        });
    
        return {
          x,
          person,
          ...toRefs(person), // ...toRefs只是把第一层拆了出来作为一个对象
        };
      },
    };
    </script>
    
    <style>
    </style> 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50

    readonly 与 shallowReadonly

    • readonly: 让一个响应式数据变为只读的(深只读)。
    • shallowReadonly:让一个响应式数据变为只读的(浅只读)。
    • 应用场景:不希望数据被修改时

    组件示例

    <template>
      <h4>当前求和为:{{ sum }}</h4>
      <button @click="sum++">点我x+1</button>
      <hr />
      <h2>姓名:{{ name }}</h2>
      <h2>年龄:{{ age }}</h2>
      <h2>薪资:{{ job.j1.salary }}K</h2>
      <button @click="name += '~'">修改姓名</button>
      <button @click="age++">修改年龄</button>
      <button @click="job.j1.salary++">修改薪资</button>
    </template>
    
    <script>
    import { ref, reactive, toRefs, readonly, shallowReadonly } from "vue";
    export default {
      name: "Demo",
      setup() {
        let sum = ref(0);
        let person = reactive({
          name: "张三",
          age: 18,
          job: {
            j1: {
              salary: 20,
            },
          },
        });
    
        // person = readonly(person)  // 前面用readonly进行接收了 
        // 有可能数据是别的组件的,你可以收到,但是你不能修改
        person = shallowReadonly(person)  
    
        // sum = readonly(sum)
        // sum = shallowReadonly(sum)
    
    
        return {
          sum,
          ...toRefs(person), // ...toRefs只是把第一层拆了出来作为一个对象
        };
      },
    };
    </script>
    
    <style>
    </style> 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46

    toRaw 与 markRaw

    toRaw

    • 作用:将一个由reactive生成的响应式对象转为普通对象
    • 使用场景:用于读取响应式对象对应的普通对象,对这个普通对象的所有操作,不会引起页面更新。

    markRaw

    • 作用:标记一个对象,使其永远不会再成为响应式对象。对象里面的数据真的被修改了,但已经不是响应式的了,不会在页面中呈现!
    • 应用场景:
      1. 有些值不应被设置为响应式的,例如复杂的第三方类库等。
      2. 当渲染具有不可变数据源的大列表时,跳过响应式转换可以提高性能。

    组件示例

    <template>
      <h4>当前求和为:{{ sum }}</h4>
      <button @click="sum++">点我x+1</button>
      <hr />
      <h2>姓名:{{ name }}</h2>
      <h2>年龄:{{ age }}</h2>
      <h2>薪资:{{ job.j1.salary }}K</h2>
      <h2 v-show="person.car">座驾信息{{ person.car }}</h2>
      <button @click="name += '~'">修改姓名</button>
      <button @click="age++">修改年龄</button>
      <button @click="job.j1.salary++">修改薪资</button>
      <!-- 输出最原始的person -->
      <button @click="showRawPerson">输出最原始的person</button>
      <button @click="addCar">给人添加一台车</button>
      <button @click="person.car.name += '!'">换车名</button>
      <button @click="person.car.price++">换车名</button>
      <button @click="addPrice">点我变换价格</button>
    </template>
    
    <script>
    import { ref, reactive, toRefs, toRaw, markRaw } from "vue";
    export default {
      name: "Demo",
      setup() {
        let sum = ref(0);
        let person = reactive({
          name: "张三",
          age: 18,
          job: {
            j1: {
              salary: 20,
            },
          },
          // car: {},
        });
    
        function showRawPerson() {
          const p = toRaw(person); // p不是响应式的了
          p.age++; // 无效
          // console.log(person)
          console.log(p);
        }
    
        function addCar() {
          let car = { name: "奔驰", price: 40 };
          // person.car = car; // 往person中添加的任何东西都是响应式的
          person.car = markRaw(car);
        }
    
        function addPrice() {
          person.car.price += 1;
          console.log(person.car.price); // 价格一直在改变,但不是响应式的,页面不进行更新
        }
    
        return {
          sum,
          person, // 将整个person传出去,这个person对象里面的内容变化,页面就会跟着变,因为其是响应式的
          ...toRefs(person), // ...toRefs只是把第一层拆了出来作为一个对象
          showRawPerson,
          addCar,
          addPrice
        };
      },
    };
    </script>
    
    <style>
    </style> 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68

    customRef

    • 作用:创建一个自定义的 ref,并对其依赖项跟踪和更新触发进行显式控制。
    • 实现一种防抖的效果

    App组件

    
    
    
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46

    provide 与 inject

    在这里插入图片描述

    • 作用:实现祖与后代组件间通信
    • 套路:父组件有一个 provide 选项来提供数据,后代组件有一个 inject 选项来开始使用这些数据
    • 具体写法:
    1. 祖组件中:
    
       ```js
       setup(){
       	......
           let car = reactive({name:'奔驰',price:'40万'})
           provide('car',car)
           ......
       }
    
    
    2. 后代组件中:
    
       ```js
       setup(props,context){
       	......
           const car = inject('car')
           return {car}
       	......
       }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    父组件

    
    
    
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

    子组件

    
    
    
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    响应式数据的判断

    • isRef: 检查一个值是否为一个 ref 对象
    • isReactive: 检查一个对象是否是由 reactive 创建的响应式代理
    • isReadonly: 检查一个对象是否是由 readonly 创建的只读代理
    • isProxy: 检查一个对象是否是由 reactive 或者 readonly 方法创建的代理

    Composition API 的优势

    Options API 存在的问题

    • 使用传统OptionsAPI中,新增或者修改一个需求,就需要分别在data,methods,computed里修改 。

    Composition API 的优势

    • 我们可以更加优雅的组织我们的代码,函数。让相关功能的代码更加有序的组织在一起。
    https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/bc0be8211fc54b6c941c036791ba4efe~tplv-k3u1fbpfcp-watermark.image
    
    https://p9-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/6cc55165c0e34069a75fe36f8712eb80~tplv-k3u1fbpfcp-watermark.image
    
    • 1
    • 2
    • 3

    在这里插入图片描述

    在这里插入图片描述

    新的组件

    Fragment

    • 在Vue2中: 组件必须有一个根标签
    • 在Vue3中: 组件可以没有根标签, 内部会将多个标签包含在一个Fragment虚拟元素中
    • 好处: 减少标签层级, 减小内存占用
      在这里插入图片描述

    Teleport

    • 什么是Teleport?—— Teleport 是一种能够将我们的组件html结构移动到指定位置的技术。
      
      	

    我是一个弹窗

    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    父组件

    
    
    
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    子组件

    
    
    
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55

    Suspense

    • 等待异步组件时渲染一些额外内容,让应用有更好的用户体验
    • 使用步骤:
      ① 异步引入组件
    // JS
    import {defineAsyncComponent} from 'vue'
    const Child = defineAsyncComponent(()=>import('./components/Child.vue'))
    
    • 1
    • 2
    • 3

    ② 使用Suspense包裹组件,并配置好defaultfallback

    <template>
    	<div class="app">
    		<h3>我是App组件</h3>
    		<Suspense>
    			<template v-slot:default>
    				<Child/>
    			</template>
    			<template v-slot:fallback>
    				<h3>加载中.....</h3>
    			</template>
    		</Suspense>
    	</div>
    </template>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    父组件

    <template>
      <div class="app">
        <h3>我是App组件()</h3>
        <Suspense>
          <!-- default -->
          <template v-slot:default>
            <Child />
          </template>
    
          <template v-slot:fallback>
            <h3>加载中,稍等....</h3>
          </template>
        </Suspense>
      </div>
    </template>
    
    <script>
    // import Child from "./components/Child.vue";  静态引入
    import { defineAsyncComponent } from "vue";
    // 动态引入 异步引入
    const Child = defineAsyncComponent(() => import("./components/Child.vue"));
    export default {
      name: "App",
      components: { Child },
    };
    </script>
    
    <style>
    .app {
      background-color: gray;
      padding: 10px;
    }
    </style>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33

    子组件

    <template>
      <div class="child">
        <h3>我是Child组件</h3>
        {{ sum }}
      </div>
    </template>
    
    <script>
    import { ref } from "vue";
    export default {
      name: "Child",
      async setup() {
        let sum = ref(0);
        let p = new Promise((resolve, reject) => {
          setTimeout(() => {
            resolve({ sum });
          }, 1000);
        });
        return await p; // await 返回成功的结果 可以用Promise
      },
    };
    </script>
    
    <style>
    .child {
      background-color: skyblue;
      padding: 10px;
    }
    </style>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29

    await返回成功的结果,可以用Promise

    其他

    全局API的转移

    • Vue 2.x 有许多全局 API 和配置。例如:① 注册全局组件、② 注册全局指令等
    //注册全局组件
    Vue.component('MyButton', {
      data: () => ({
        count: 0
      }),
      template: ''
    })
    
    //注册全局指令
    Vue.directive('focus', {
      inserted: el => el.focus()
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • Vue3.0中对这些API做出了调整:将全局的API,即:Vue.xxx调整到应用实例(app)上
      对比图

      2.x 全局 API(Vue3.x 实例 API (app)
      Vue.config.xxxxapp.config.xxxx
      Vue.config.productionTip移除
      Vue.componentapp.component
      Vue.directiveapp.directive
      Vue.mixinapp.mixin
      Vue.useapp.use
      Vue.prototypeapp.config.globalProperties

    其他改变

    • data选项应始终被声明为一个函数。
    • ② 过度类名的更改:

    Vue2.x的写法

    // CSS
    .v-enter,
    .v-leave-to {
      opacity: 0;
    }
    .v-leave,
    .v-enter-to {
      opacity: 1;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    Vue3.x的写法

    // CSS
    .v-enter-from,
    .v-leave-to {
      opacity: 0;
    }
    
    .v-leave-from,
    .v-enter-to {
      opacity: 1;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 移除keyCode作为 v-on 的修饰符,同时也不再支持config.keyCodes
    • 移除v-on.native修饰符

    移除该修饰符 Vue3的解决方案
    父组件中绑定事件

    <my-component
      v-on:close="handleComponentEvent"
      v-on:click="handleNativeClickEvent"
    />
    
    • 1
    • 2
    • 3
    • 4

    子组件中声明自定义事件

    <script>
      export default {
        emits: ['close']
      }
    </script>
    
    • 1
    • 2
    • 3
    • 4
    • 5

    使用emits选项去指定自定义事件,没有指定的,默认是原生事件

    • 移除过滤器(filter):过滤器虽然这看起来很方便,但它需要一个自定义语法,打破大括号内表达式是 “只是 JavaScript” 的假设,这不仅有学习成本,而且有实现成本!建议用方法调用或计算属性去替换过滤器。

    祝福语

    在这里插入图片描述

  • 相关阅读:
    10 种3D 建模技术
    DSPE-PEG-DBCO,DBCO-PEG-DSPE,磷脂-聚乙二醇-二苯并环辛炔科研实验用
    E. Hanging Hearts #831 div1+2
    生成m3u8视频:批量剪辑与分割的完美结合
    进程与线程
    Blazor前后端框架Known-V1.2.15
    MySQL的Explain总结
    2022.6.30-2022.7.3 Three.js 学习笔记
    网约车司机被取代?百度获得中国首个全无人驾驶出租车服务许可证
    2023面试知识点一
  • 原文地址:https://blog.csdn.net/A13526_/article/details/126232560