shallowReactive:只处理对象最外层属性的响应式(浅响应式)。
shallowRef:只处理基本数据类型的响应式, 不进行对象的响应式处理。
什么时候使用?
<template>
<h4>当前求和为:{{sum}}h4>
<button @click="sum++">点我++button>
<hr>
<h2>姓名:{{name}}h2>
<h2>年龄:{{age}}h2>
<h2>薪资:{{job.j1.salary}}Kh2>
<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)
// person = shallowReadonly(person)
// sum = readonly(sum)
// sum = shallowReadonly(sum)
//返回一个对象(常用)
return {
sum,
...toRefs(person)
}
}
}
script>
reactive
生成的响应式对象转为普通对象。<template>
<h4>当前求和为:{{sum}}h4>
<button @click="sum++">点我++button>
<hr>
<h2>姓名:{{name}}h2>
<h2>年龄:{{age}}h2>
<h2>薪资:{{job.j1.salary}}Kh2>
<h3 v-show="person.car">座驾信息:{{person.car}}h3>
<button @click="name+='~'">修改姓名button>
<button @click="age++">增长年龄button>
<button @click="job.j1.salary++">涨薪button>
<button @click="showRawPerson">输出最原始的personbutton>
<button @click="addCar">给人添加一台车button>
<button @click="person.car.name+='!'">换车名button>
<button @click="changePrice">换价格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
}
}
})
function showRawPerson(){
const p = toRaw(person)
p.age++
console.log(p)
}
function addCar(){
let car = {name:'奔驰',price:40}
person.car = markRaw(car)
}
function changePrice(){
person.car.price++
console.log(person.car.price)
}
//返回一个对象(常用)
return {
sum,
person,
...toRefs(person),
showRawPerson,
addCar,
changePrice
}
}
}
script>
作用:创建一个自定义的 ref,并对其依赖项跟踪和更新触发进行显式控制。
实现防抖效果:
<template>
<input type="text" v-model="keyWord">
<h3>{{keyWord}}h3>
template>
<script>
import {ref,customRef} from 'vue'
export default {
name: 'App',
setup() {
//自定义一个ref——名为:myRef
function myRef(value,delay){
let timer
return customRef((track,trigger)=>{
return {
get(){
console.log(`有人从myRef这个容器中读取数据了,我把${value}给他了`)
track() //通知Vue追踪value的变化(提前和get商量一下,让他认为这个value是有用的)
return value
},
set(newValue){
console.log(`有人把myRef这个容器中数据改为了:${newValue}`)
clearTimeout(timer)
timer = setTimeout(()=>{
value = newValue
trigger() //通知Vue去重新解析模板
},delay)
},
}
})
}
// let keyWord = ref('hello') //使用Vue提供的ref
let keyWord = myRef('hello',500) //使用程序员自定义的ref
return {keyWord}
}
}
script>
作用:实现祖与后代组件间通信
套路:父组件有一个 provide
选项来提供数据,后代组件有一个 inject
选项来开始使用这些数据
具体写法:
祖组件中:
setup(){
......
let car = reactive({name:'奔驰',price:'40万'})
provide('car',car)
......
}
后代组件中:
setup(props,context){
......
const car = inject('car')
return {car}
......
}
App.vue
<template>
<div class="app">
<h3>我是App组件(祖),{{name}}--{{price}}h3>
<Child/>
div>
template>
<script>
import { reactive,toRefs,provide } from 'vue'
import Child from './components/Child.vue'
export default {
name:'App',
components:{Child},
setup(){
let car = reactive({name:'奔驰',price:'40W'})
provide('car',car) //给自己的后代组件传递数据
return {...toRefs(car)}
}
}
script>
<style>
.app{
background-color: gray;
padding: 10px;
}
style>
Child.vue
<template>
<div class="child">
<h3>我是Child组件(子)h3>
<Son/>
div>
template>
<script>
import {inject} from 'vue'
import Son from './Son.vue'
export default {
name:'Child',
components:{Son},
/* setup(){
let x = inject('car')
console.log(x,'Child-----')
} */
}
script>
<style>
.child{
background-color: skyblue;
padding: 10px;
}
style>
Son.vue
<template>
<div class="son">
<h3>我是Son组件(孙),{{car.name}}--{{car.price}}h3>
div>
template>
<script>
import {inject} from 'vue'
export default {
name:'Son',
setup(){
let car = inject('car')
return {car}
}
}
script>
<style>
.son{
background-color: orange;
padding: 10px;
}
style>
reactive
创建的响应式代理readonly
创建的只读代理reactive
或者 readonly
方法创建的代理 import {ref, reactive,toRefs,readonly,isRef,isReactive,isReadonly,isProxy } from 'vue'
export default {
name:'App',
setup(){
let car = reactive({name:'奔驰',price:'40W'})
let sum = ref(0)
let car2 = readonly(car)
console.log(isRef(sum))
console.log(isReactive(car))
console.log(isReadonly(car2))
console.log(isProxy(car))
console.log(isProxy(sum))
return {...toRefs(car)}
}
}