vue3新特征和所有的属性,方法汇总及其对应源码分析
zhezhongyun 2025-08-07 00:02 61 浏览
vue3新特征汇总与源码分析
(备注:vue3使用typescript编写)
何为应用?
const app = Vue.createApp({})
app就是一个应用。
应用的配置和应用的API就是app应用的属性和方法。
1.应用配置:
- performance:开启浏览器的性能监控。值为true|false
- optionMergeStrategies:option选项的合并策略
- globalProperties:扩展实例的属性和方法
- isCustomElement:判断哪些标签为自定义组件
- errorHandler:错误时的处理函数
- warnHandler:警示时的处理函数
export interface AppConfig {
// @private
readonly isNativeTag?: (tag: string) => boolean
performance: boolean
optionMergeStrategies: Record<string, OptionMergeFunction>
globalProperties: Record<string, any>
isCustomElement: (tag: string) => boolean
errorHandler?: (
err: unknown,
instance: ComponentPublicInstance | null,
info: string
) => void
warnHandler?: (
msg: string,
instance: ComponentPublicInstance | null,
trace: string
) => void
}
2.应用API:
- version:版本号
- config:应用的配置信息
- use:引入插件
- mixin:引入混合器
- component:引入组件
- directive:引入指令
- mount:挂载组件
- unmount:卸载组件
- provide:全局提供状态,与inject结合使用
export interface App<HostElement = any> {
version: string
config: AppConfig
use(plugin: Plugin, ...options: any[]): this
mixin(mixin: ComponentOptions): this
component(name: string): Component | undefined
component(name: string, component: Component): this
directive(name: string): Directive | undefined
directive(name: string, directive: Directive): this
mount(
rootContainer: HostElement | string,
isHydrate?: boolean
): ComponentPublicInstance
unmount(rootContainer: HostElement | string): void
provide<T>(key: InjectionKey<T> | string, value: T): this
// internal, but we need to expose these for the server-renderer and devtools
_uid: number
_component: ConcreteComponent
_props: Data | null
_container: HostElement | null
_context: AppContext
}
2.1应用上下文:
export function createAppContext(): AppContext {
return {
app: null as any,
config: {
isNativeTag: NO,
performance: false,
globalProperties: {},
optionMergeStrategies: {},
isCustomElement: NO,
errorHandler: undefined,
warnHandler: undefined
},
mixins: [],
components: {},
directives: {},
provides: Object.create(null)
}
}
3.全局API:
4.选项:
Data:
- data:值类型为Function,
- props:值类型为object|array
- computed:值类型为{ [key: string]: Function | { get: Function, set: Function } }
- methods:值类型为{ [key: string]: Function }
- watch:值类型为{ [key: string]: string | Function | Object | Array}
- emits:类型为Array | Object
DOM:
- template:值类型为string
- render:值类型为Function
生命周期钩子:
beforeCreate,
created,
beforeMount,
mounted,
beforeUpdate,
updated,
beforeUnmount,
uonUnmounted,
activated,
deactivated,
renderTracked,
renderTriggered,
errorCaptured
资源:
- directives:值类型为Object
- components:值类型为Object
- 自定义指令的参数选项,特别说明:
- 参数为对象:
- export interface ObjectDirective<T = any, V = any> {
created?: DirectiveHook<T, null, V>
beforeMount?: DirectiveHook<T, null, V>
mounted?: DirectiveHook<T, null, V>
beforeUpdate?: DirectiveHook<T, VNode<any, T>, V>
updated?: DirectiveHook<T, VNode<any, T>, V>
beforeUnmount?: DirectiveHook<T, null, V>
unmounted?: DirectiveHook<T, null, V>
getSSRProps?: SSRDirectiveHook
} - //指令的钩子函数的参数:
export type DirectiveHook<T = any, Prev = VNode<any, T> | null, V = any> = (
el: T,
//绑定的修饰符,属性,值,指令名称等信息在binding里面
binding: DirectiveBinding<V>,
vnode: VNode<any, T>,
prevVNode: Prev
) => void - export interface DirectiveBinding<V = any> {
instance: ComponentPublicInstance | null
value: V
oldValue: V | null
arg?: string
modifiers: DirectiveModifiers
dir: ObjectDirective<any, V>
}
组合:
- mixins:值类型为Array
- extends:值类型为Object | Function
- provide:值类型为Object | () => Object
- inject:值类型为Array | { [key: string]: string | Symbol | Object }
- setup:值类型为Function
杂项:
- name:值类型为string。组件名称
- delimiters:
- inheritAttrs:值类型为boolean
5.实例属性(property):
- this.$data:
- this.$props:
- this.$el:
- this.$options:
- this.$root:
- this.$parent:
- this.$slots:
- this.$refs:
- this.$attrs:
6.实例方法:
- this.$watch():
- this.$emit():
- this.$forceUpdate():
- this.$nextTick():
7.指令:
- v-text:处理文本
- v-html:处理html
- v-show:显示与隐藏,dom已经渲染好
- v-if:满足条件才开始渲染,否则不渲染
- v-else:满足条件才开始渲染,否则不渲染
- v-else-if:满足条件才开始渲染,否则不渲染
- v-for:遍历列表
- v-on:绑定事件
- v-bind:绑定属性
- v-model:绑定表单的变量
- v-slot:绑定插槽具名,缩写:#
- v-one:标签只渲染一次
- v-is:绑定动态组件
- v-pre:
- v-cloak:
8.特殊指令:
- key:处理列表循环的key
- ref:处理标签的ref。类似于id
- is:处理动态组件,绑定组件的命名
9.内置组件:
- component:自定义组件,与:is一起使用
- transition:过度组件
- transition-groud:过度组件组
- keep-alive:缓存不活动的组件
- slot:插槽组件
- teleport:转移组件
10.响应式API:
import {reactive,readonly} from 'vue'
响应性基础api:
- reactive: 实现响应式对象,包括嵌套对象都是响应式对象,返回proxy代理对象
- readonly:实现对象只读,包括嵌套对象都为只读,返回proxy代理对象
- isProxy:判断是否是代理对象
- isReactive:判断是否是响应式对象
- isReadonly:判断是否是只读对象
- toRaw:入参为响应式对象,返回原始对象。
- markRaw:标志原始对象,不能再实现响应式对象。
- shallowReactive:浅相应式对象,只有第一层属性为响应式对象,嵌套对象不属于响应式对象。
- shallowReadonly:浅只读对象,只有第一层属性为只读对象,嵌套对象不属于只读对象,可以修改嵌套对象的属性。
Refs
- ref:接受一个内部值并返回一个响应式且可变的 ref 对象。ref 对象具有指向内部值的单个 property .value
- unref:返回对象的原始值
- toRef:可以用来为源响应式对象上的 property 新创建一个 ref。然后可以将 ref 传递出去,从而保持对其源 property 的响应式连接。(即把响应式对象的单个属性转换成ref对象)
- toRefs:将响应式对象转换为普通对象,其中结果对象的每个 property 都是指向原始对象相应 property 的ref。(即把响应式对象的每个属性都转换成ref对象)
- isRef:判断是否是Ref对象
- customRef:创建一个自定义的ref函数
- shallowRef:创建一个 ref,它跟踪自己的 .value 更改,但不会使其值成为响应式的。
- triggerRef:手动执行与 shallowRef 关联的任何副作用
Computed:使用 getter 函数,并为从 getter 返回的值返回一个不变的响应式 ref 对象。
watch:
watchEffect:在响应式地跟踪其依赖项时立即运行一个函数,并在更改依赖项时重新运行它。
ReactiveEffect,
ReactiveEffectOptions,
DebuggerEvent,
TrackOpTypes,
TriggerOpTypes,
Ref,
ComputedRef,
WritableComputedRef,
UnwrapRef,
ShallowUnwrapRef,
WritableComputedOptions,
ToRefs,
DeepReadonly
11.组合式API:
- setup:值类型为Function。在创建组件之前执行,返回值自动嵌入实例的属性中
- 生命周期钩子(只能在setup函数中使用): 只能在 setup() 期间同步使用
- onBeforeCreate,
onCreated,
onBeforeMount,
onMounted,
onBeforeUpdate,
onUpdated,
onBeforeUnmount,
onUnmounted,
onActivated,
onDeactivated,
onRenderTracked,
onRenderTriggered,
onErrorCaptured
- provide/inject :
- getCurrentInstance getCurrentInstance 只能在 setup 或生命周期钩子中调用
setup?: (
this: void,
props: Props &
UnionToIntersection<ExtractOptionProp<Mixin>> &
UnionToIntersection<ExtractOptionProp<Extends>>,
ctx: SetupContext<E>
) => Promise<RawBindings> | RawBindings | RenderFunction | void
name?: string
template?: string | object // can be a direct DOM node
// Note: we are intentionally using the signature-less `Function` type here
// since any type with signature will cause the whole inference to fail when
// the return expression contains reference to `this`.
// Luckily `render()` doesn't need any arguments nor does it care about return
// type.
render?: Function
components?: Record<string, Component>
directives?: Record<string, Directive>
inheritAttrs?: boolean
emits?: (E | EE[]) & ThisType<void>
// TODO infer public instance type based on exposed keys
expose?: string[]
serverPrefetch?(): Promise<any>
const {
// composition
mixins,
extends: extendsOptions,
// state
data: dataOptions,
computed: computedOptions,
methods,
watch: watchOptions,
provide: provideOptions,
inject: injectOptions,
// assets
components,
directives,
// lifecycle
beforeMount,
mounted,
beforeUpdate,
updated,
activated,
deactivated,
beforeDestroy,
beforeUnmount,
destroyed,
unmounted,
render,
renderTracked,
renderTriggered,
errorCaptured,
// public API
expose
} = options
相关推荐
- Python入门学习记录之一:变量_python怎么用变量
-
写这个,主要是对自己学习python知识的一个总结,也是加深自己的印象。变量(英文:variable),也叫标识符。在python中,变量的命名规则有以下三点:>变量名只能包含字母、数字和下划线...
- python变量命名规则——来自小白的总结
-
python是一个动态编译类编程语言,所以程序在运行前不需要如C语言的先行编译动作,因此也只有在程序运行过程中才能发现程序的问题。基于此,python的变量就有一定的命名规范。python作为当前热门...
- Python入门学习教程:第 2 章 变量与数据类型
-
2.1什么是变量?在编程中,变量就像一个存放数据的容器,它可以存储各种信息,并且这些信息可以被读取和修改。想象一下,变量就如同我们生活中的盒子,你可以把东西放进去,也可以随时拿出来看看,甚至可以换成...
- 绘制学术论文中的“三线表”具体指导
-
在科研过程中,大家用到最多的可能就是“三线表”。“三线表”,一般主要由三条横线构成,当然在变量名栏里也可以拆分单元格,出现更多的线。更重要的是,“三线表”也是一种数据记录规范,以“三线表”形式记录的数...
- Python基础语法知识--变量和数据类型
-
学习Python中的变量和数据类型至关重要,因为它们构成了Python编程的基石。以下是帮助您了解Python中的变量和数据类型的分步指南:1.变量:变量在Python中用于存储数据值。它们充...
- 一文搞懂 Python 中的所有标点符号
-
反引号`无任何作用。传说Python3中它被移除是因为和单引号字符'太相似。波浪号~(按位取反符号)~被称为取反或补码运算符。它放在我们想要取反的对象前面。如果放在一个整数n...
- Python变量类型和运算符_python中变量的含义
-
别再被小名词坑哭了:Python新手常犯的那些隐蔽错误,我用同事的真实bug拆给你看我记得有一次和同事张姐一起追查一个看似随机崩溃的脚本,最后发现罪魁祸首竟然是她把变量命名成了list。说实话...
- 从零开始:深入剖析 Spring Boot3 中配置文件的加载顺序
-
在当今的互联网软件开发领域,SpringBoot无疑是最为热门和广泛应用的框架之一。它以其强大的功能、便捷的开发体验,极大地提升了开发效率,成为众多开发者构建Web应用程序的首选。而在Spr...
- Python中下划线 ‘_’ 的用法,你知道几种
-
Python中下划线()是一个有特殊含义和用途的符号,它可以用来表示以下几种情况:1在解释器中,下划线(_)表示上一个表达式的值,可以用来进行快速计算或测试。例如:>>>2+...
- 解锁Shell编程:变量_shell $变量
-
引言:开启Shell编程大门Shell作为用户与Linux内核之间的桥梁,为我们提供了强大的命令行交互方式。它不仅能执行简单的文件操作、进程管理,还能通过编写脚本实现复杂的自动化任务。无论是...
- 一文学会Python的变量命名规则!_python的变量命名有哪些要求
-
目录1.变量的命名原则3.内置函数尽量不要做变量4.删除变量和垃圾回收机制5.结语1.变量的命名原则①由英文字母、_(下划线)、或中文开头②变量名称只能由英文字母、数字、下画线或中文字所组成。③英文字...
- 更可靠的Rust-语法篇-区分语句/表达式,略览if/loop/while/for
-
src/main.rs://函数定义fnadd(a:i32,b:i32)->i32{a+b//末尾表达式}fnmain(){leta:i3...
- C++第五课:变量的命名规则_c++中变量的命名规则
-
变量的命名不是想怎么起就怎么起的,而是有一套固定的规则的。具体规则:1.名字要合法:变量名必须是由字母、数字或下划线组成。例如:a,a1,a_1。2.开头不能是数字。例如:可以a1,但不能起1a。3....
- Rust编程-核心篇-不安全编程_rust安全性
-
Unsafe的必要性Rust的所有权系统和类型系统为我们提供了强大的安全保障,但在某些情况下,我们需要突破这些限制来:与C代码交互实现底层系统编程优化性能关键代码实现某些编译器无法验证的安全操作Rus...
- 探秘 Python 内存管理:背后的神奇机制
-
在编程的世界里,内存管理就如同幕后的精密操控者,确保程序的高效运行。Python作为一种广泛使用的编程语言,其内存管理机制既巧妙又复杂,为开发者们提供了便利的同时,也展现了强大的底层控制能力。一、P...
- 一周热门
- 最近发表
- 标签列表
-
- HTML 教程 (33)
- HTML 简介 (35)
- HTML 实例/测验 (32)
- HTML 测验 (32)
- JavaScript 和 HTML DOM 参考手册 (32)
- HTML 拓展阅读 (30)
- HTML文本框样式 (31)
- HTML滚动条样式 (34)
- HTML5 浏览器支持 (33)
- HTML5 新元素 (33)
- HTML5 WebSocket (30)
- HTML5 代码规范 (32)
- HTML5 标签 (717)
- HTML5 标签 (已废弃) (75)
- HTML5电子书 (32)
- HTML5开发工具 (34)
- HTML5小游戏源码 (34)
- HTML5模板下载 (30)
- HTTP 状态消息 (33)
- HTTP 方法:GET 对比 POST (33)
- 键盘快捷键 (35)
- 标签 (226)
- opacity 属性 (32)
- transition 属性 (33)
- 1-1. 变量声明 (31)
