vue3新特征和所有的属性,方法汇总及其对应源码分析
ztj100 2024-11-26 11:14 19 浏览 0 评论
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
相关推荐
- 人生苦短,我要在VSCode里面用Python
-
轻沉发自浅度寺量子位出品|公众号QbitAI在程序员圈子里,VisualStudioCode(以下简称VSCode)可以说是目前最火的代码编辑器之一了。它是微软出品的一款可扩展的轻量...
- 亲测可用:Pycharm2019.3专业版永久激活教程
-
概述随着2020年的到来,又有一批Pycharm的激活码到期了,各位同仁估计也是在到处搜索激活方案,在这里,笔者为大家收录了一个永久激活的方案,亲测可用,欢迎下载尝试:免责声明本项目只做个人学习研究之...
- Python新手入门很简单(python教程入门)
-
我之前学习python走过很多的歧途,自学永远都是瞎猫碰死耗子一样,毫无头绪。后来心里一直都有一个做头条知识分享的梦,希望自己能够帮助曾经类似自己的人,于是我来了,每天更新5篇Python文章,喜欢的...
- Pycharm的设置和基本使用(pycharm运行设置)
-
这篇文章,主要是针对刚开始学习python语言,不怎么会使用pycharm的童鞋们;我来带领大家详细了解下pycharm页面及常用的一些功能,让大家能通过此篇文章能快速的开始编写python代码。一...
- 依旧是25年最拔尖的PyTorch实用教程!堪比付费级内容!
-
我真的想知道作者到底咋把PyTorch教程整得这么牛的啊?明明在内容上已经足以成为付费教材了,但作者偏要免费开源给大家学习!...
- 手把手教你 在Pytorch框架上部署和测试关键点人脸检测项目DBFace
-
这期教向大家介绍仅仅1.3M的轻量级高精度的关键点人脸检测模型DBFace,并手把手教你如何在自己的电脑端进行部署和测试运行,运行时bug解决。01.前言前段时间DBFace人脸检测库横空出世,...
- 进入Python的世界02外篇-Pycharm配置Pyqt6
-
为什么这样配置,要开发带UI的python也只能这样了,安装过程如下:一安装工具打开终端:pipinstallPyQt6PyQt6-tools二打开设置并汉化点击plugin,安装汉化插件,...
- vs code如何配置使用Anaconda(vscode调用anaconda库)
-
上一篇文章中(Anaconda使用完全指南),我们能介绍了Anaconda的安装和使用,以及如何在pycharm中配置Anaconda。本篇,将继续介绍在vscode中配置conda...
- pycharm中conda解释器无法配置(pycharm配置anaconda解释器)
-
之前用的好好的pycharm正常配置解释器突然不能用了?可以显示有这个环境然后确认后可以conda正在配置解释器,但是进度条结束后还是不成功!!试过了pycharm重启,pycharm重装,anaco...
- Volta:跨平台开发者的福音,统一前端js工具链从未如此简单!
-
我们都知道现在已经进入了Rust时代,不仅很多终端常用的工具都被rust重写了,而且现在很多前端工具也开始被Rust接手了,这不,现在就出现了一款JS工具管理工具,有了它,你可以管理多版本的js工具,...
- 开发者的福音,ElectronEgg: 新一代桌面应用开发框架
-
今天给大家介绍一个开源项目electron-egg。如果你是一个JS的前端开发人员,以前面对这项任务桌面应用开发在时,可能会感到无从下手,甚至觉得这是一项困难的挑战。ElectronEgg的出现,它能...
- 超强经得起考验的低代码开发平台Frappe
-
#挑战30天在头条写日记#开始进行管理软件的开发来讲,如果从头做起不是不可以,但选择一款免费的且经得起时间考验的低代码开发平台是非常有必要的,将大幅提升代码的质量、加快开发的效率、以及提高程序的扩展性...
- 一文带你搞懂Vue3 底层源码(vue3核心源码解析)
-
作者:妹红大大转发链接:https://mp.weixin.qq.com/s/D_PRIMAD6i225Pn-a_lzPA前言vue3出来有一段时间了。今天正式开始记录一下梗vue3.0.0-be...
- 基于小程序 DSL(微信、支付宝)的,可扩展的多端研发框架
-
Mor(发音为/mr/,类似more),是饿了么开发的一款基于小程序DSL的,可扩展的多端研发框架,使用小程序原生DSL构建,使用者只需书写一套(微信或支付宝)小程序,就可以通过Mor...
你 发表评论:
欢迎- 一周热门
- 最近发表
-
- 人生苦短,我要在VSCode里面用Python
- 亲测可用:Pycharm2019.3专业版永久激活教程
- Python新手入门很简单(python教程入门)
- Pycharm的设置和基本使用(pycharm运行设置)
- 依旧是25年最拔尖的PyTorch实用教程!堪比付费级内容!
- 手把手教你 在Pytorch框架上部署和测试关键点人脸检测项目DBFace
- 进入Python的世界02外篇-Pycharm配置Pyqt6
- vs code如何配置使用Anaconda(vscode调用anaconda库)
- pycharm中conda解释器无法配置(pycharm配置anaconda解释器)
- Volta:跨平台开发者的福音,统一前端js工具链从未如此简单!
- 标签列表
-
- idea eval reset (50)
- vue dispatch (70)
- update canceled (42)
- order by asc (53)
- spring gateway (67)
- 简单代码编程 贪吃蛇 (40)
- transforms.resize (33)
- redisson trylock (35)
- 卸载node (35)
- np.reshape (33)
- torch.arange (34)
- npm 源 (35)
- vue3 deep (35)
- win10 ssh (35)
- vue foreach (34)
- idea设置编码为utf8 (35)
- vue 数组添加元素 (34)
- std find (34)
- tablefield注解用途 (35)
- python str转json (34)
- java websocket客户端 (34)
- tensor.view (34)
- java jackson (34)
- vmware17pro最新密钥 (34)
- mysql单表最大数据量 (35)