01.ref 全家桶

基本用法 ref

<template>
<h1>{{data.name}}</h1>
</template>
<script setup lang="ts">
import {ref} from "vue"
const data = ref({name:'zhangsan'})
data.name.value = "lisi"
</script>

使用泛型

<template>
<h1>{{data.name}}</h1>
</template>
<script setup lang="ts">
import {ref} from "vue"
import type {Ref} from "vue"
type Data = {name:string}
var data:Ref<Data> = ref({name:'zhangsan'})
</script>

使用复杂的 interface

<template>
<h1>{{data.name}}</h1>
</template>
<script setup lang="ts">
import {ref} from "vue"
import type {Ref} from "vue"
interface Data {
 name:string
}
var data:Ref<Data> = ref({name:'zhangsan'})
</script>

浅层响应式 shallowRef

用法同 ref , shallowRef 不会更新视图,即不会调用 triggerRef 而 Ref 函数每次变化都会触发 triggerRef;

<template>
<h1>{{data.name}}</h1>
</template>
<script setup lang="ts">
import {shallowRef} from "vue"
const data = shallowRef<any>({name:'zhangsan'})
setTimeout(()=>{data.value.name='lisi',3000) // 视图不会更新
</script>

强制更新 triggerRef

<template>
<h1>{{data.name}}</h1>
<h1>{{data2.name}}</h1>
</template>
<script setup lang="ts">
import {ref,shallowRef,triggerRef} from "vue"
const data = shallowRef<any>({name:'zhangsan'})
setTimeout(()=>{
   data.value.name="lilsi"
   triggerRef(data)
},3000) // 视图不会更新
</script>
暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇