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

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

qiyuwang 2025-03-10 20:01 4 浏览 0 评论

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
globalProperties: Record
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 {
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(key: InjectionKey | 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 {
    created?: DirectiveHook
    beforeMount?: DirectiveHook
    mounted?: DirectiveHook
    beforeUpdate?: DirectiveHook<T, VNode, V>
    updated?: DirectiveHook<T, VNode, V>
    beforeUnmount?: DirectiveHook
    unmounted?: DirectiveHook
    getSSRProps?: SSRDirectiveHook
    }
  • //指令的钩子函数的参数:
    export type DirectiveHook<T = any, Prev = VNode | null, V = any> = (
    el: T,
    //绑定的修饰符,属性,值,指令名称等信息在binding里面
    binding: DirectiveBinding,
    vnode: VNode,
    prevVNode: Prev
    ) => void
  • export interface DirectiveBinding {
    instance: ComponentPublicInstance | null
    value: V
    oldValue: V | null
    arg?: string
    modifiers: DirectiveModifiers
    dir: ObjectDirective
    }

组合:

  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> &
UnionToIntersection<ExtractOptionProp>,
ctx: SetupContext
) => Promise | 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
directives?: Record
inheritAttrs?: boolean
emits?: (E | EE[]) & ThisType
// TODO infer public instance type based on exposed keys
expose?: string[]
serverPrefetch?(): Promise

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

相关推荐

在Word中分栏设置页码一页两个页码的技巧!

施老师:在正常情况下,Word文档中一页只会出现一个页码。但在某种情况下,比如说:用了分栏后,我们希望一页中出现两个页码,那应该如何实现呢?今天,就由宁双学好网施老师来为大家讲一下,利用域来实现一页两...

如何在关键时刻向上自荐(如何在关键时刻做出正确选择)

抓住机会,挺身而出有种时刻叫“关键时刻”,关键时刻,作为一个认为自己有能力的、训练有素的人,应该考虑挺身而出,甚至应该不考虑就挺身而出。...

WPS Word:跨页的文档表格,快速调整为一页。#Excel

如何快速将跨页的文档表格调整为一页?需要根据两种情况分别处理。如果表格所有行的行高相同,调整为一页的方法有两种。第一种方法是将光标移动到表格内,然后将鼠标移动到表格右下角的方框处,按住鼠标左键向上拖动...

word文档插入下一页分节符(word下一页分页符)

在word文档中,对文档页面进行分页是特别常见的操作,其中的下一页分节符也是用得比较多的,但是一些人不太清楚在哪里设置,也不知道它具体能实现的功能是什么。接下来看看如何在word文档中插入下一页分节符...

word文档如何设置某一页纸张的方向

word文档页面方向有横向和纵向,纵向是默认的纸张方向,有时我们需要将页面设置为横向,或只设置其中某一页方向,应该怎么操作呢?一起来看看下面的详细介绍第一步:...

word怎么单独设置一页为横向(word2019怎样设置单独一页为横向)

word里面其中一页可以改为横向的吗?经过实际操作发现是完全可以的。...

Word如何设置分栏,如何一页内容同时显示一栏和两栏

我们使用Word文档,有时需要用到两栏的排版,甚至一页内容同时包含一栏和两栏的排版,这种格式怎么设置呢?具体步骤如下:首先是两栏排版的设置,直接点击Word文件上方工具栏【布局】,选择【分栏】下面的【...

Word怎么分页?这三个方法可以帮到你

我们不仅可以利用Word编辑文档,还可以编辑文集呢。但是有时候会出现两个部分的文章长短不一,我们需要对文档进行分页处理。这样可以方便我们对文档进行其他操作。那么Word怎么分页呢?大家可以采用下面这...

Word内容稍超一页,如何优化至单页打印?

如何将两页纸的内容,缩到一页打印呢?有时候一页纸多一点内容,我们完全可以缩一下,放到一页来打印。...

[word] word 表格如何跨行显示表头、标题

word表格如何跨行显示表头、标题在Word中的表格如果过长的话,会跨行显示在另一页,如果想要在其它页面上也显示表头,更直观的查看数据。难道要一个个复制表头吗?当然不是,教你简单的方法操作设置Wo...

Word表格跨页如何续上表?(word如何让表格跨页不断掉)

长文档的表格跨页时,你会发现页末空白太多了,这时要怎么调整?选中整张表格,右击【表格属性】,点击【行】选项,之后勾选【允许跨页断行】,点击确定即可解决空白问题。...

Word怎么连续自动生成页码,操作步骤来了!

Word怎么连续自动生成页码,操作步骤来了!...

word文档怎么把两页合并成一页内容?教你4种方法

word怎么把两页合并成一页?word怎么把两页合并成一页?用四种方法演示一下。·方法一:把这一个文档合并成一页,按ctrl加a全选文档,然后右键点击段落,弹出的界面行距改成固定值,磅值可以改小一点,...

如何将Word中的一页的纸张方向设置为横向?这里提供详细步骤

默认情况下,MicrosoftWord将页面定向为纵向视图。虽然这在大多数情况下都很好,但你可能拥有在横向视图中看起来更好的页面或页面组。以下是实现这一目标的两种方法。无论使用哪种方法,请注意,如果...

Word横竖混排你会玩吗?(word横排竖排混合)

我们在用Word排版的时候,一般都是竖版格式,但偶尔会需要到一些特殊的版式要求,比如文档中插入的一个表格,横向的内容比较多,这时就需要用到横版,否则表格显示不全。这种横竖版混排的要求,在Word20...

取消回复欢迎 发表评论: