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
#setup
一个组件选项,在创建组件之前执行,一旦 props
被解析,并作为组合式 API 的入口点
- 入参:
{Data} props
{SetupContext} context
- 类型声明:
interface Data {
[key: string]: unknown
}
interface SetupContext {
attrs: Data
slots: Slots
emit: (event: string, ...args: unknown[]) => void
}
function setup(props: Data, context: SetupContext): Data
TIP
若要获取传递给 setup()
的参数的类型推断,请使用 defineComponent 是需要的。
- 示例:
使用模板:
<!-- MyBook.vue -->
<template>
<div>{{ readersNumber }} {{ book.title }}</div>
</template>
<script>
import { ref, reactive } from 'vue'
export default {
setup() {
const readersNumber = ref(0)
const book = reactive({ title: 'Vue 3 Guide' })
// expose to template
return {
readersNumber,
book
}
}
}
</script>
使用渲染函数:
// MyBook.vue
import { h, ref, reactive } from 'vue'
export default {
setup() {
const readersNumber = ref(0)
const book = reactive({ title: 'Vue 3 Guide' })
// 请注意,我们需要在这里显式地暴露ref值
return () => h('div', [readersNumber.value, book.title])
}
}
- 参考:组合式 API
setup
#生命周期钩子
可以使用直接导入的 onX
函数注册生命周期钩子:
import { onMounted, onUpdated, onUnmounted } from 'vue'
const MyComponent = {
setup() {
onMounted(() => {
console.log('mounted!')
})
onUpdated(() => {
console.log('updated!')
})
onUnmounted(() => {
console.log('unmounted!')
})
}
}
这些生命周期钩子注册函数只能在 setup()
期间同步使用,因为它们依赖于内部全局状态来定位当前活动实例 (此时正在调用其 setup()
的组件实例)。在没有当前活动实例的情况下调用它们将导致错误。
组件实例上下文也是在生命周期钩子的同步执行期间设置的,因此在生命周期钩子内同步创建的侦听器和计算属性也会在组件卸载时自动删除。
选项 API 生命周期选项和组合式 API 之间的映射
beforeCreate
-> usesetup()
created
-> usesetup()
beforeMount
->onBeforeMount
mounted
->onMounted
beforeUpdate
->onBeforeUpdate
updated
->onUpdated
beforeUnmount
->onBeforeUnmount
unmounted
->onUnmounted
errorCaptured
->onErrorCaptured
renderTracked
->onRenderTracked
renderTriggered
->onRenderTriggered
- 参考:组合式 API 生命周期钩子
#Provide / Inject
provide
和 inject
启用依赖注入。只有在使用当前活动实例的 setup()
期间才能调用这两者。
- 类型声明:
interface InjectionKey<T> extends Symbol {}
function provide<T>(key: InjectionKey<T> | string, value: T): void
// without default value
function inject<T>(key: InjectionKey<T> | string): T | undefined
// with default value
function inject<T>(key: InjectionKey<T> | string, defaultValue: T): T
Vue 提供了一个 InjectionKey
接口,该接口是扩展 Symbol
的泛型类型。它可用于在提供者和消费者之间同步注入值的类型:
import { InjectionKey, provide, inject } from 'vue'
const key: InjectionKey<string> = Symbol()
provide(key, 'foo') // 提供非字符串值将导致错误
const foo = inject(key) // foo 的类型: string | undefined
如果使用字符串 key 或非类型化 symbols,则需要显式声明注入值的类型:
const foo = inject<string>('foo') // string | undefined
- 参考
:
- Provide / Inject
- 组合式 API Provide / Inject
#getCurrentInstance
getCurrentInstance
允许访问对高级使用或库创建者有用的内部组件实例。
import { getCurrentInstance } from 'vue'
const MyComponent = {
setup() {
const internalInstance = getCurrentInstance()
internalInstance.appContext.config.globalProperties // access to globalProperties
}
}
getCurrentInstance
仅在安装或生命周期挂钩期间有效。
在安装程序或生命周期挂钩之外使用时,请在setup
上调用getCurrentInstance()
,并改用该实例。
const MyComponent = {
setup() {
const internalInstance = getCurrentInstance() // works
const id = useComponentId() // works
const handleClick = () => {
getCurrentInstance() // doesn't work
useComponentId() // doesn't work
internalInstance // works
}
onMounted(() => {
getCurrentInstance() // works
})
return () =>
h(
'button',
{
onClick: handleClick
},
`uid: ${id}`
)
}
}
// also works if called on a composable
function useComponentId() {
return getCurrentInstance().uid
}