百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 技术文章 > 正文

vue3新特征和所有的属性,方法汇总及其对应源码分析

zhezhongyun 2025-08-07 00:02 43 浏览

vue3新特征汇总与源码分析

(备注:vue3使用typescript编写)

何为应用?

const app = Vue.createApp({})

app就是一个应用。

应用的配置和应用的API就是app应用的属性和方法。

1.应用配置:

  1. performance:开启浏览器的性能监控。值为true|false
  2. optionMergeStrategies:option选项的合并策略
  3. globalProperties:扩展实例的属性和方法
  4. isCustomElement:判断哪些标签为自定义组件
  5. errorHandler:错误时的处理函数
  6. 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:

  1. version:版本号
  2. config:应用的配置信息
  3. use:引入插件
  4. mixin:引入混合器
  5. component:引入组件
  6. directive:引入指令
  7. mount:挂载组件
  8. unmount:卸载组件
  9. 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:

  1. data:值类型为Function,
  2. props:值类型为object|array
  3. computed:值类型为{ [key: string]: Function | { get: Function, set: Function } }
  4. methods:值类型为{ [key: string]: Function }
  5. watch:值类型为{ [key: string]: string | Function | Object | Array}
  6. emits:类型为Array | Object


DOM:

  1. template:值类型为string
  2. render:值类型为Function

生命周期钩子:

beforeCreate,
created,
beforeMount,
mounted,
beforeUpdate,
updated,
beforeUnmount,
uonUnmounted,
activated,
deactivated,
renderTracked,
renderTriggered,
errorCaptured

资源:

  1. directives:值类型为Object
  2. 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>
    }

组合:

  1. mixins:值类型为Array
  2. extends:值类型为Object | Function
  3. provide:值类型为Object | () => Object
  4. inject:值类型为Array | { [key: string]: string | Symbol | Object }
  5. setup:值类型为Function

杂项:

  1. name:值类型为string。组件名称
  2. delimiters:
  3. inheritAttrs:值类型为boolean

5.实例属性(property):

  1. this.$data:
  2. this.$props:
  3. this.$el:
  4. this.$options:
  5. this.$root:
  6. this.$parent:
  7. this.$slots:
  8. this.$refs:
  9. this.$attrs:

6.实例方法:

  1. this.$watch():
  2. this.$emit():
  3. this.$forceUpdate():
  4. this.$nextTick():

7.指令:

  1. v-text:处理文本
  2. v-html:处理html
  3. v-show:显示与隐藏,dom已经渲染好
  4. v-if:满足条件才开始渲染,否则不渲染
  5. v-else:满足条件才开始渲染,否则不渲染
  6. v-else-if:满足条件才开始渲染,否则不渲染
  7. v-for:遍历列表
  8. v-on:绑定事件
  9. v-bind:绑定属性
  10. v-model:绑定表单的变量
  11. v-slot:绑定插槽具名,缩写:#
  12. v-one:标签只渲染一次
  13. v-is:绑定动态组件
  14. v-pre:
  15. v-cloak:

8.特殊指令:

  1. key:处理列表循环的key
  2. ref:处理标签的ref。类似于id
  3. is:处理动态组件,绑定组件的命名

9.内置组件:

  1. component:自定义组件,与:is一起使用
  2. transition:过度组件
  3. transition-groud:过度组件组
  4. keep-alive:缓存不活动的组件
  5. slot:插槽组件
  6. teleport:转移组件

10.响应式API:

import {reactive,readonly} from 'vue'

响应性基础api:

  1. reactive: 实现响应式对象,包括嵌套对象都是响应式对象,返回proxy代理对象
  2. readonly:实现对象只读,包括嵌套对象都为只读,返回proxy代理对象
  3. isProxy:判断是否是代理对象
  4. isReactive:判断是否是响应式对象
  5. isReadonly:判断是否是只读对象
  6. toRaw:入参为响应式对象,返回原始对象。
  7. markRaw:标志原始对象,不能再实现响应式对象。
  8. shallowReactive:浅相应式对象,只有第一层属性为响应式对象,嵌套对象不属于响应式对象。
  9. shallowReadonly:浅只读对象,只有第一层属性为只读对象,嵌套对象不属于只读对象,可以修改嵌套对象的属性。

Refs

  1. ref:接受一个内部值并返回一个响应式且可变的 ref 对象。ref 对象具有指向内部值的单个 property .value
  2. unref:返回对象的原始值
  3. toRef:可以用来为源响应式对象上的 property 新创建一个 ref。然后可以将 ref 传递出去,从而保持对其源 property 的响应式连接。(即把响应式对象的单个属性转换成ref对象)
  4. toRefs:将响应式对象转换为普通对象,其中结果对象的每个 property 都是指向原始对象相应 property 的ref。(即把响应式对象的每个属性都转换成ref对象)
  5. isRef:判断是否是Ref对象
  6. customRef:创建一个自定义的ref函数
  7. shallowRef:创建一个 ref,它跟踪自己的 .value 更改,但不会使其值成为响应式的。
  8. triggerRef:手动执行与 shallowRef 关联的任何副作用

Computed:使用 getter 函数,并为从 getter 返回的值返回一个不变的响应式 ref 对象。

watch:

watchEffect:在响应式地跟踪其依赖项时立即运行一个函数,并在更改依赖项时重新运行它。

ReactiveEffect,
ReactiveEffectOptions,
DebuggerEvent,
TrackOpTypes,
TriggerOpTypes,
Ref,
ComputedRef,
WritableComputedRef,
UnwrapRef,
ShallowUnwrapRef,
WritableComputedOptions,
ToRefs,
DeepReadonly

11.组合式API:

  1. setup:值类型为Function。在创建组件之前执行,返回值自动嵌入实例的属性中
  2. 生命周期钩子(只能在setup函数中使用): 只能在 setup() 期间同步使用
  • onBeforeCreate,
    onCreated,
    onBeforeMount,
    onMounted,
    onBeforeUpdate,
    onUpdated,
    onBeforeUnmount,
    onUnmounted,
    onActivated,
    onDeactivated,
    onRenderTracked,
    onRenderTriggered,
    onErrorCaptured
  1. provide/inject :
  2. 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

相关推荐

VSCode中值得推荐的常用的23个高效前端插件(工具篇)(一)

VSCode是我们前端开发的一个强大的IDE,所以选择趁手好用的插件是提高开发效率,然后剩下的时间用来摸鱼是很有必要滴。工具篇(23)Chinese(Simplified)vscode我们都知道是...

高级前端进阶,用gulp提升你的开发效率

前言:这两天动手配置了一下gulp,发现gulp配置简单,构建速度快,在某些使用场景下还是个不错的选择,本文从零开始构建,到最后打包发布到生成环境。通过本文可以快速上手gulp,文末附送github源...

Chrome 110 3大新特性!CSS支持画中画!

大家好,很高兴又见面了,我是"前端进阶",由我带着大家一起关注前端前沿、深入前端底层技术,大家一起进步,也欢迎大家关注、点赞、收藏、转发!今天带着大家一起看看最新发布的Chrome1...

用html中If语句——判断ie浏览器的版本

if语句的代码的语法非常简单,,就是一个if判断语句来判断浏览器的类型和版本,应用类似<!--[iflteIE6]>和<![endif]-->语法结构包孕起来...

谷歌浏览器怎么开启无痕浏览_谷歌浏览器怎么开启无痕浏览模式

很多用户在使用谷歌浏览器时,不希望留下任何上痕迹,开启无痕浏览器是最好的选择。这个模式下可以更好的保护个人隐私记录,给你带来更加安全的冲浪体验,接下来就给大家详细介绍下谷歌浏览器的无痕浏览模式,希望对...

Linux命令那么多,其实只需要记住这些就足够了!

你好,这里是网络技术联盟站,我是瑞哥。Linux命令行是一个强大且灵活的工具,可以极大地提高用户的工作效率和系统管理能力。我们都知道,Linux命令非常多,但是在实际的工作中,日常使用到的命令并不多,...

Linux如何查看文件_linux如何查看文件大小

Linux如何查看目录下的所有文件?用ls(list)查看当前目录下的所有文件和子目录。Ls查看目录下的文件,怎么区分是目录还是文件呢?第一种方式,我们可以通过颜色来区分目录和文件。默认情况下,目录显...

Linux系统man命令使用详解_linux man命令详解

man命令是在Linux和Unix系统上用于查看系统手册页(manualpages)的工具。手册页提供了关于系统命令、函数和文件的详细文档。命令语法:man[选项][命令或主题]参数:[选项]...

linux ps命令详解_linux中ps

linux中ps只显示进程的静态快照,及瞬间的进程状态,它拥有众多的风格,可分为3组:UNIX风格,BSD风格,GNU风格,本文介绍UNIX风格的ps指令。参数ps[-aefFly][-ppid...

如何在 Linux 上查找系统硬件信息?hwinfo命令很强大!

hwinfo是一个功能强大的硬件信息查询工具,专为Linux系统设计。它能够提供系统中几乎所有硬件组件的详细信息,包括但不限于CPU、内存、硬盘、网络设备、USB设备、显卡、声卡等。与其他常...

Linux Shell 入门教程(二):常用命令大全与使用技巧

在上一节《理解Linux与Shell》中,我们了解了Linux是什么、Shell是什么以及常见的Shell类型。这一篇,我们将正式动手操作,掌握使用频率最高、最实用的Linux命令...

SpringBoot应用部署神器:可视化服务管理脚本让运维更轻松

在SpringBoot应用的生产环境部署中,传统的手动启停服务方式不仅效率低下,还容易出错。今天分享一个功能强大的可视化服务管理脚本,让SpringBoot应用的部署和运维变得简单高效。痛点分析:传统...

一次虚拟机性能问题导致的应用故障

最近我负责维护的一套语音平台出了问题。故障现象据客户反馈是转入IVR以后没有正常响应,客户无奈挂机了。老实说,刚开始接到用户反馈的时候,我是不太相信的。我们的系统平时运行运行很稳定,客户的并发数不大,...

linux中的常用命令_linux常用命令及含义

linux中的常用命令linux中的命令统称shell命令shell是一个命令行解释器,将用户命令解析为操作系统所能理解的指令,实现用户与操作系统的交互shell终端:我们平时输入命令,执行程序的那个...

linux学习笔记——常用命令-文件处理命令

ls目录处理命令:ls全名:list命令路径:/bin/ls执行权限:所有用户ls–ala--alll–long-i查看i节点ls–i查看i节点命令名称:mkdir命令英文原意:m...