Vue3 教程
- 第1节:Vue 3.0 安装
- 第2节:Vue 3.0 介绍
- 第3节:Vue 3.0 应用&组件实例
- 第4节:Vue 3.0 模板语法
- 第5节:Vue 3.0 Data Property和方法
- 第6节:Vue 3.0 计算属性和侦听器
- 第7节:Vue 3.0 Class与Style绑定
- 第8节:Vue 3.0 条件渲染
- 第9节:Vue 3.0 列表渲染
- 第10节:Vue 3.0 事件处理
- 第11节:Vue 3.0 表单输入绑定
- 第12节:Vue 3.0 组件基础
- 第13节:Vue 3.0 组件注册
- 第14节:Vue 3.0 Props
- 第15节:Vue 3.0 非Prop的Attribute
- 第16节:Vue 3.0 自定义事件
- 第17节:Vue 3.0 插槽
- 第18节:Vue 3.0 提供/注入
- 第19节:Vue 3.0 动态组件&异步组件
- 第20节:Vue 3.0 模板引用
- 第21节:Vue 3.0 处理边界情况
- 第22节:Vue 3.0 过渡&动画概述
- 第23节:Vue 3.0 进入过渡&离开过渡
- 第24节:Vue 3.0 列表过渡
- 第25节:Vue 3.0 状态过渡
- 第26节:Vue 3.0 混入
- 第27节:Vue 3.0 自定义指令
- 第28节:Vue 3.0 Teleport
- 第29节:Vue 3.0 渲染函数
- 第30节:Vue 3.0 插件
- 第31节:Vue 3.0 响应性 深入响应性原理
- 第32节:Vue 3.0 响应性 基础
- 第33节:Vue 3.0 响应式 计算和侦听
- 第34节:Vue 3.0 组合式API 介绍
- 第35节:Vue 3.0 组合式API Setup
- 第36节:Vue 3.0 组合式API 生命周期钩子
- 第37节:Vue 3.0 组合式API 提供/注入
- 第38节:Vue 3.0 组合式API 模板引用
- 第39节:Vue 3.0 渲染机制和优化
- 第40节:Vue2中的更改检测警告
- 第41节:Vue 3.0 单文件组件
- 第42节:Vue 3.0 测试
- 第43节:Vue 3.0 TypeScript支持
- 第44节:Vue 3.0 Mobile
- 第45节:Vue 3.0 路由
- 第46节:Vue 3.0 状态管理
- 第47节:Vue 3.0 服务端渲染
- 第48节:Vue 3.0 应用配置
- 第49节:Vue 3.0 应用API
- 第50节:Vue 3.0 全局API
- 第51节:Vue 3.0 选项 Data
- 第52节:Vue 3.0 选项 DOM
- 第53节:Vue 3.0 选项 生命周期钩子
- 第54节:Vue 3.0 选项/资源
- 第55节:Vue 3.0 选项 组合
- 第56节:Vue 3.0 选项 杂项
- 第57节:Vue 3.0 实例property
- 第58节:Vue 3.0 实例方法
- 第59节:Vue 3.0 指令
- 第60节:Vue 3.0 特殊指令
- 第61节:Vue 3.0 内置组件
- 第62节:Vue 3.0 响应性基础 API
- 第63节:Vue 3.0 响应性API Refs
- 第64节:Vue 3.0 响应性API Computed与watch
- 第65节:Vue 3.0 组合式API
Vue 3.0 响应性API Computed与watch
#computed
使用 getter 函数,并为从 getter 返回的值返回一个不变的响应式 ref 对象。
const count = ref(1)
const plusOne = computed(() => count.value + 1)
console.log(plusOne.value) // 2
plusOne.value++ // error
或者,它可以使用具有 get
和 set
函数的对象来创建可写的 ref 对象。
const count = ref(1)
const plusOne = computed({
get: () => count.value + 1,
set: val => {
count.value = val - 1
}
})
plusOne.value = 1
console.log(count.value) // 0
类型声明:
// read-only
function computed<T>(getter: () => T): Readonly<Ref<Readonly<T>>>
// writable
function computed<T>(options: { get: () => T; set: (value: T) => void }): Ref<T>
#watchEffect
在响应式地跟踪其依赖项时立即运行一个函数,并在更改依赖项时重新运行它。
const count = ref(0)
watchEffect(() => console.log(count.value))
// -> logs 0
setTimeout(() => {
count.value++
// -> logs 1
}, 100)
类型声明:
function watchEffect(
effect: (onInvalidate: InvalidateCbRegistrator) => void,
options?: WatchEffectOptions
): StopHandle
interface WatchEffectOptions {
flush?: 'pre' | 'post' | 'sync' // default: 'pre'
onTrack?: (event: DebuggerEvent) => void
onTrigger?: (event: DebuggerEvent) => void
}
interface DebuggerEvent {
effect: ReactiveEffect
target: any
type: OperationTypes
key: string | symbol | undefined
}
type InvalidateCbRegistrator = (invalidate: () => void) => void
type StopHandle = () => void
参考:watchEffect
指南
#watch
watch
API 与选项式 API this.$watch (以及相应的 watch 选项) 完全等效。watch
需要侦听特定的 data 源,并在单独的回调函数中副作用。默认情况下,它也是惰性的——即,回调是仅在侦听源发生更改时调用。
- 与 watchEffect 比较,
watch
允许我们:- 惰性地执行副作用;
- 更具体地说明应触发侦听器重新运行的状态;
- 访问侦听状态的先前值和当前值。
#侦听一个单一源
侦听器 data 源可以是返回值的 getter 函数,也可以是 ref:
// 侦听一个getter
const state = reactive({ count: 0 })
watch(
() => state.count,
(count, prevCount) => {
/* ... */
}
)
// 直接侦听一个ref
const count = ref(0)
watch(count, (count, prevCount) => {
/* ... */
})
#侦听多个源
侦听器还可以使用数组同时侦听多个源:
watch([fooRef, barRef], ([foo, bar], [prevFoo, prevBar]) => {
/* ... */
})
#与 watchEffect
共享行为
watch
与 watchEffect
在手动停止,副作用无效 (将 onInvalidate
作为第三个参数传递给回调),flush timing 和 debugging 有共享行为。
类型声明:
// 侦听单一源
function watch<T>(
source: WatcherSource<T>,
callback: (
value: T,
oldValue: T,
onInvalidate: InvalidateCbRegistrator
) => void,
options?: WatchOptions
): StopHandle
// 侦听多个源
function watch<T extends WatcherSource<unknown>[]>(
sources: T
callback: (
values: MapSources<T>,
oldValues: MapSources<T>,
onInvalidate: InvalidateCbRegistrator
) => void,
options? : WatchOptions
): StopHandle
type WatcherSource<T> = Ref<T> | (() => T)
type MapSources<T> = {
[K in keyof T]: T[K] extends WatcherSource<infer V> ? V : never
}
// 参见 `watchEffect` 类型声明共享选项
interface WatchOptions extends WatchEffectOptions {
immediate?: boolean // default: false
deep?: boolean
}