关于指令(directive)

属性绑定、事件绑定和v-modal底层都是通过指令(directive)实现的,那么什么是指令呢?我们一起看看Directive的定义吧。

//文件 ./src/directives/index.ts

export interface Directive<T = Element> {
(ctx: DirectiveContext<T>): (() => void) | void
}

指令(directive)其实就是一个接受参数类型为DirectiveContext并且返回cleanup

函数或啥都不返回的函数。那么DirectiveContext有是如何的呢?

//文件 ./src/directives/index.ts

export interface DirectiveContext<T = Element> {
el: T
get: (exp?: string) => any // 获取表达式字符串运算后的结果
effect: typeof rawEffect // 用于添加副作用函数
exp: string // 表达式字符串
arg?: string // v-bind:value或:value中的value, v-on:click或@click中的click
modifiers?: Record<string, true> // @click.prevent中的prevent
ctx: Context
}

深入v-bind的工作原理

walk方法在解析模板时会遍历元素的特性集合el.attributes,当属性名称name匹配v-bind:时,则调用processDirective(el, 'v-bind', value, ctx)对属性名称进行处理并转发到对应的指令函数并执行。

//文件 ./src/walk.ts

// 为便于阅读,我将与v-bind无关的代码都删除了
const processDirective = (
el: Element,
raw, string, // 属性名称
exp: string, // 属性值:表达式字符串
ctx: Context
) => {
let dir: Directive
let arg: string | undefined
let modifiers: Record<string, true> | undefined // v-bind有且仅有一个modifier,那就是camel if (raw[0] == ':') {
dir = bind
arg = raw.slice(1)
}
else {
const argIndex = raw.indexOf(':')
// 由于指令必须以`v-`开头,因此dirName则是从第3个字符开始截取
const dirName = argIndex > 0 ? raw.slice(2, argIndex) : raw.slice(2)
// 优先获取内置指令,若查找失败则查找当前上下文的指令
dir = builtInDirectives[dirName] || ctx.dirs[dirName]
arg = argIndex > 0 ? raw.slice(argIndex) : undefined
} if (dir) {
// 由于ref不是用于设置元素的属性,因此需要特殊处理
if (dir === bind && arg === 'ref') dir = ref
applyDirective(el, dir, exp, ctx, arg, modifiers)
}
}

processDirective根据属性名称匹配相应的指令和抽取入参后,就会调用applyDirective来通过对应的指令执行操作。

//文件 ./src/walk.ts

const applyDirective = (
el: Node,
dir: Directive<any>,
exp: string,
ctx: Context,
arg?: string
modifiers?: Record<string, true>
) => {
const get = (e = exp) => evaluate(ctx.scope, e, el)
// 指令执行后可能会返回cleanup函数用于执行资源释放操作,或什么都不返回
const cleanup = dir({
el,
get,
effect: ctx.effect,
ctx,
exp,
arg,
modifiers
}) if (cleanup) {
// 将cleanup函数添加到当前上下文,当上下文销毁时会执行指令的清理工作
ctx.cleanups.push(cleanup)
}
}

现在我们终于走到指令bind执行阶段了

//文件 ./src/directives/bind.ts

// 只能通过特性的方式赋值的属性
const forceAttrRE = /^(spellcheck|draggable|form|list|type)$/ export const bind: Directive<Element & { _class?: string }> => ({
el,
get,
effect,
arg,
modifiers
}) => {
let prevValue: any
if (arg === 'class') {
el._class = el.className
} effect(() => {
let value = get()
if (arg) {
// 用于处理v-bind:style="{color:'#fff'}" 的情况 if (modifiers?.camel) {
arg = camelize(arg)
}
setProp(el, arg, value, prevValue)
}
else {
// 用于处理v-bind="{style:{color:'#fff'}, fontSize: '10px'}" 的情况 for (const key in value) {
setProp(el, key, value[key], prevValue && prevValue[key])
}
// 删除原视图存在,而当前渲染的新视图不存在的属性
for (const key in prevValue) {
if (!value || !(key in value)) {
setProp(el, key, null)
}
}
}
prevValue = value
})
} const setProp = (
el: Element & {_class?: string},
key: string,
value: any,
prevValue?: any
) => {
if (key === 'class') {
el.setAttribute(
'class',
normalizeClass(el._class ? [el._class, value] : value) || ''
)
}
else if (key === 'style') {
value = normalizeStyle(value)
const { style } = el as HTMLElement
if (!value) {
// 若`:style=""`则移除属性style
el.removeAttribute('style')
}
else if (isString(value)) {
if (value !== prevValue) style.cssText = value
}
else {
// value为对象的场景
for (const key in value) {
setStyle(style, key, value[key])
}
// 删除原视图存在,而当前渲染的新视图不存在的样式属性
if (prevValue && !isString(prevValue)) {
for (const key in prevValue) {
if (value[key] == null) {
setStyle(style, key, '')
}
}
}
}
}
else if (
!(el instanceof SVGElement) &&
key in el &&
!forceAttrRE.test(key)) {
// 设置DOM属性(属性类型可以是对象)
el[key] = value
// 留给`v-modal`使用的
if (key === 'value') {
el._value = value
}
} else {
// 设置DOM特性(特性值仅能为字符串类型) /* 由于`<input v-modal type="checkbox">`元素的属性`value`仅能存储字符串,
* 通过`:true-value`和`:false-value`设置选中和未选中时对应的非字符串类型的值。
*/
if (key === 'true-value') {
;(el as any)._trueValue = value
}
else if (key === 'false-value') {
;(el as any)._falseValue = value
}
else if (value != null) {
el.setAttribute(key, value)
}
else {
el.removeAttribute(key)
}
}
} const importantRE = /\s*!important/ const setStyle = (
style: CSSStyleDeclaration,
name: string,
val: string | string[]
) => {
if (isArray(val)) {
val.forEach(v => setStyle(style, name, v))
}
else {
if (name.startsWith('--')) {
// 自定义属性
style.setProperty(name, val)
}
else {
if (importantRE.test(val)) {
// 带`!important`的属性
style.setProperty(
hyphenate(name),
val.replace(importantRE, ''),
'important'
)
}
else {
// 普通属性
style[name as any] = val
}
}
}
}

总结

通过本文我们以后不单可以使用v-bind:style绑定单一属性,还用通过v-bind一次过绑定多个属性,虽然好像不太建议这样做>_<

后续我们会深入理解v-on事件绑定的工作原理,敬请期待。

petite-vue源码剖析-属性绑定`v-bind`的工作原理的更多相关文章

  1. petite-vue源码剖析-双向绑定`v-model`的工作原理

    前言 双向绑定v-model不仅仅是对可编辑HTML元素(select, input, textarea和附带[contenteditable=true])同时附加v-bind和v-on,而且还能利用 ...

  2. petite-vue源码剖析-事件绑定`v-on`的工作原理

    在书写petite-vue和Vue最舒服的莫过于通过@click绑定事件,而且在移除元素时框架会帮我们自动解除绑定.省去了过去通过jQuery的累赘.而事件绑定在petite-vue中就是一个指令(d ...

  3. Spark源码剖析(八):stage划分原理与源码剖析

    引言 对于Spark开发人员来说,了解stage的划分算法可以让你知道自己编写的spark application被划分为几个job,每个job被划分为几个stage,每个stage包括了你的哪些代码 ...

  4. Vue源码解析---数据的双向绑定

    本文主要抽离Vue源码中数据双向绑定的核心代码,解析Vue是如何实现数据的双向绑定 核心思想是ES5的Object.defineProperty()和发布-订阅模式 整体结构 改造Vue实例中的dat ...

  5. [Vue源码]一起来学Vue双向绑定原理-数据劫持和发布订阅

    有一段时间没有更新技术博文了,因为这段时间埋下头来看Vue源码了.本文我们一起通过学习双向绑定原理来分析Vue源码.预计接下来会围绕Vue源码来整理一些文章,如下. 一起来学Vue双向绑定原理-数据劫 ...

  6. 源码剖析@ApiImplicitParam对@RequestParam的required属性的侵入性

    问题起源 使用SpringCloud构建项目时,使用Swagger生成相应的接口文档是推荐的选项,Swagger能够提供页面访问,直接在网页上调试后端系统的接口, 非常方便.最近却遇到了一个有点困惑的 ...

  7. petite-vue源码剖析-逐行解读@vue/reactivity之reactive

    在petite-vue中我们通过reactive构建上下文对象,并将根据状态渲染UI的逻辑作为入参传递给effect,然后神奇的事情发生了,当状态发生变化时将自动触发UI重新渲染.那么到底这是怎么做到 ...

  8. 逐行剖析Vue源码(一)——写在最前面

    1. 前言 博主作为一名前端开发,日常开发的技术栈是Vue,并且用Vue开发也有一年多了,对其用法也较为熟练了,但是对各种用法和各种api使用都是只知其然而不知其所以然,因此,有时候在排查bug的时候 ...

  9. 菜鸟nginx源码剖析 框架篇(一) 从main函数看nginx启动流程(转)

    俗话说的好,牵牛要牵牛鼻子 驾车顶牛,处理复杂的东西,只要抓住重点,才能理清脉络,不至于深陷其中,不能自拔.对复杂的nginx而言,main函数就是“牛之鼻”,只要能理清main函数,就一定能理解其中 ...

随机推荐

  1. RPC调用获取参数值

    本文以 RPC 获取百度登录password加密值为例: 涉及的知识点有: 1.js调试,寻找加密代码 2. 浏览器本地代码替换 3. js自执行函数 4. 插桩 5. RPC 远程调用 6. pyt ...

  2. 《Effective TypeScript》条款22 - 类型收缩

    本文主要记录书中关于TypeScript类型收缩的内容 本文主要内容如下 类型收缩的一些方法 条件判断 抛错误 instanceof 和 in 属性检查 "标签联合"或" ...

  3. Uwl.Admin.Core开源框架(二) 使用QuartzNet

    Uwl.Admin.Core中使用QuartzNet定时任务模块: 本文负责讲解RabbitMQ的使用 Uwl.Admin.Core使用的技术有: *.Async和Await 异步编程 *.Repos ...

  4. PHP中英文混合字符串处理

    转载请注明来源:https://www.cnblogs.com/hookjc/ function cut_str($string, $sublen, $start = 0, $code = 'utf- ...

  5. axios取消接口请求

    axios取消请求 这里就是分析一下接口请求需要被取消时的一些操作 因为我是用vue写的项目,所以标配用的是axios,怎么在axios中取消已经发送的请求呢? 1.在这之前我们还是先介绍一下原生js ...

  6. Docker之LNMP分布式容器部署

    Docker之LNMP分布式容器部署 目录 Docker之LNMP分布式容器部署 一.项目模拟 1. 项目环境 2. 服务器环境 3. 任务需求 二.Linux系统基础镜像 三.Nginx 1. 建立 ...

  7. 重新认识Appium

    一.重新认识Appium   找到了学习资料,却不知道怎么实现!!! 要如何实现呢? Appium完整案例值得参考:手把手搭建环境,其中安装和配置Mave这部分有点老了. 首先下载maven 官网地址 ...

  8. Jetpack的ViewModel与LiveData

    本文基于SDK 29 一.ViewModel与LiveData的作用: 1.viewModel: 数据共享,屏幕旋转不丢失数据,并且在Activity与Fragment之间共享数据. 2.LiveDa ...

  9. Solution -「NOI.AC 省选膜你赛」寄蒜几盒

    题目 题意简述   给定一个含有 \(n\) 个顶点的凸多边形( \(n\) 是偶数),对于每一对相对的边(即中间有 \(\frac{n}2-1\) 条其它边),延长它们以将平面分割为多块,并把包含原 ...

  10. linux大工程 - 我要一个属于自己的回收站

    我要开始装13了 'rm -rf 是一个很"粗鲁"的命令,就像windows的shift+delete,删除的文件是无法找回的(当然,除了数据恢复软件,但是很麻烦,很费时)' '为 ...