vue3学习笔记(第二天)

## 8.生命周期 

- `beforeCreate`===>`setup()`
  - `created`=======>`setup()`
  - `beforeMount` ===>`onBeforeMount`
  - `mounted`=======>`onMounted`
  - `beforeUpdate`===>`onBeforeUpdate`
  - `updated` =======>`onUpdated`
  - `beforeUnmount` ==>`onBeforeUnmount`
  - `unmounted` =====>`onUnmounted`

<template>
        <div>当前的值是{{hh}}</div>
        <button @click="hh++">点我+1</button>
        <br>
     
</template>

<script>
import { ref } from '@vue/reactivity'
import { onBeforeMount, onBeforeUnmount, onBeforeUpdate, onMounted, onUpdated } from '@vue/runtime-core'
export default {
     name:'app',
     setup(){   
       let hh=ref(0)
        onBeforeMount(()=>{

        })
        onMounted(()=>{

        })
        onBeforeUpdate(()=>{

        })
        onUpdated(()=>{

        })
        onBeforeUnmount(()=>{

        })
        onMounted(()=>{
            
        })

        return {
            hh,
      
        }
     }  ,
     beforeCreate(){
        console.log('beforeCreate')
     },
     created(){
        console.log("created")
     },
     beforeMount(){

     },
     mounted(){

     },
     beforeUpdate(){

     },
     updated(){

     },
    beforeUnmount(){

    },
    unmounted(){

    }

      

}
</script>

9.自定义hook

本质是一个函数,把setup函数中使用的Composition API进行了封装。

import { onMounted, onUnmounted, reactive } from "vue"

const usePoint=()=>{
        let point=reactive({
            x:0,
            y:0,
        })
        const getPoint=(event)=>{
            point.x=event.pageX;
            point.y=event.pageY
        }
        onMounted(()=>{
            window.addEventListener('click',getPoint)
        })
        onUnmounted(()=>{
            window.removeEventListener('click',getPoint)
        })

        return point
}

export {usePoint}


## 10.toRef

- 作用:创建一个 ref 对象,其value值指向另一个对象中的某个属性。
- 语法:```const name = toRef(person,'name')```
- 应用:   要将响应式对象中的某个属性单独提供给外部使用时。


- 扩展:```toRefs``` 与```toRef```功能一致,但可以批量创建多个 ref 对象,语法:```toRefs(person)```

<template>
        
        <div>{{name}}</div>
        <button @click="name+='~'">嘿嘿嘿</button>
        <div>{{age}}</div>
        <button @click="age++">点我变老</button>
        <!-- <div >{{work.salary}}</div>
        <button @click="work.salary++">点我加薪</button> -->
</template>

<script>
import { reactive, toRef } from '@vue/reactivity'
export default {
     name:'app',
     setup(){   
     
        let person=reactive({
            name:'张三',
            age:22,
            work:{
                salary:4,
            }
        })



        return {    
          name:toRef(person,'name'),
          age:toRef(person,'age'),
          
// ...toRefs(person) 

}
     }   

}
</script>

# 三、其它 Composition API

## 1.shallowReactive 与 shallowRef

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

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

## 2.readonly 与 shallowReadonly

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

## 3.toRaw 与 markRaw

- toRaw:
  - 作用:将一个由```reactive```生成的<strong style="color:orange">响应式对象</strong>转为<strong style="color:orange">普通对象</strong>。
  - 使用场景:用于读取响应式对象对应的普通对象,对这个普通对象的所有操作,不会引起页面更新。
- markRaw:
  - 作用:标记一个对象,使其永远不会再成为响应式对象。
  - 应用场景:
    1. 有些值不应被设置为响应式的,例如复杂的第三方类库等。
    2. 当渲染具有不可变数据源的大列表时,跳过响应式转换可以提高性能。

## 4.customRef

- 作用:创建一个自定义的 ref,并对其依赖项跟踪和更新触发进行显式控制。

  <template>
  	<input type="text" v-model="keyword">
  	<h3>{{keyword}}</h3>
  </template>
  
  <script>
  	import {ref,customRef} from 'vue'
  	export default {
  		name:'Demo',
  		setup(){
  			// let keyword = ref('hello') //使用Vue准备好的内置ref
  			//自定义一个myRef
  			function myRef(value,delay){
  				let timer
  				//通过customRef去实现自定义
  				return customRef((track,trigger)=>{
  					return{
  						get(){
  							track() //告诉Vue这个value值是需要被“追踪”的
  							return value
  						},
  						set(newValue){
  							clearTimeout(timer)
  							timer = setTimeout(()=>{
  								value = newValue
  								trigger() //告诉Vue去更新界面
  							},delay)
  						}
  					}
  				})
  			}
  			let keyword = myRef('hello',500) //使用程序员自定义的ref
  			return {
  				keyword
  			}
  		}
  	}
  </script>

 

## 5.provide 与 inject

<img src="https://v3.cn.vuejs.org/images/components_provide.png" style="width:300px" />

- 作用:实现<strong style="color:#DD5145">祖与后代组件间</strong>通信

- 套路:父组件有一个 `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}
         ......
     }
     ```

## 6.响应式数据的判断

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

# 五、新的组件

## 1.Fragment

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

## 3.Suspense

- 等待异步组件时渲染一些额外内容,让应用有更好的用户体验

- 使用步骤:

  - 异步引入组件

    ```js
    import {defineAsyncComponent} from 'vue'
    const Child = defineAsyncComponent(()=>import('./components/Child.vue'))
    ```

  - 使用```Suspense```包裹组件,并配置好```default``` 与 ```fallback```

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值