《Vue3中的ref》

ref

接受一个内部值并返回一个响应式且可变的 ref 对象。ref 对象仅有一个 .value property,指向该内部值

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<template>
<div>
<button @click="changeMsg">change</button>
<div>{{ message }}</div>
</div>
</template>
<script setup lang="ts">
let message: string = "我是message"

const changeMsg = () => {
message = "change msg"
}
</script>
<style>
</style>

比如说,我们这样操作是无法改变 message 的值, 因为 message 不是响应式的无法被 vue 跟踪要改成 ref;

改为 ref;
Ref TS 对应的接口:

1
2
3
interface Ref<T> {
value: T;
}

注意被 ref 包装之后需要用.value来进行赋值

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<template>
<div>
<button @click="changeMsg">change</button>
<div>{{ message }}</div>
</div>
</template>
<script setup lang="ts">
import {ref,Ref} from 'vue'
let message:Ref<string> = ref("我是message")

const changeMsg = () => {
message.value = "change msg"
}
</script>
<style>
</style>

ts 两种方式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<template>
<div>
<button @click="changeMsg">change</button>
<div>{{ message }}</div>
</div>
</template>
<script setup lang="ts">
import { ref } from 'vue'
let message = ref<string | number>("我是message")

const changeMsg = () => {
message.value = "change msg"
}
</script>
<style>
</style>

isRef
判断是否是一个 ref 对象

1
2
3
4
5
6
7
8
import { ref, Ref, isRef } from "vue";
let message: Ref<string | number> = ref("我是message");
let notRef: number = 123;
const changeMsg = () => {
message.value = "change msg";
console.log(isRef(message)); //true
console.log(isRef(notRef)); //false
};

shallowRef
创建一个跟踪自身.value变化的 ref,但不会使其值也变成响应式;

比如说:修改其属性是非响应式的这样是不会改变的:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<template>
<div>
<button @click="changeMsg">change</button>
<div>{{ message }}</div>
</div>
</template>
<script setup lang="ts">
import { Ref, shallowRef } from 'vue'
type Obj = {
name: string
}
let message: Ref<Obj> = shallowRef({
name: "nagisa"
})
const changeMsg = () => {
message.value.name = 'caoysong'
}
</script>
<style>
</style>

这样是可以被监听到的修改 value

1
2
3
4
5
6
7
8
9
10
import { Ref, shallowRef } from "vue";
type Obj = {
name: string,
};
let message: Ref<Obj> = shallowRef({
name: "nagisa",
});
const changeMsg = () => {
message.value = { name: "nagisa" };
};

triggerRef
强制更新页面 DOM

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<template>
<div>
<button @click="changeMsg">change</button>
<div>{{ message }}</div>
</div>
</template>
<script setup lang="ts">
import { Ref, shallowRef,triggerRef } from 'vue'
type Obj = {
name: string
}
let message: Ref<Obj> = shallowRef({
name: "nagisa"
})
const changeMsg = () => {
message.value.name = 'caoysong'
triggerRef(message)
}
</script>
<style>
</style>

customRef
自定义 ref
customRef 是一个工厂函数要求我们返回一个对象,并且实现 get 和 set

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<script setup lang="ts">
import { Ref, shallowRef, triggerRef, customRef } from 'vue'
function Myref<T>(value: T) {
return customRef((track, trigger) => {
return {
get() {
track()
return value
},
set(newVal: T) {
console.log('set');
value = newVal
trigger()
}
}
})
}
let message = Myref('nagisa')
const changeMsg = () => {
message.value = 'caoysong'
// triggerRef(message)
}
</script>