Vue源码分析
2023-04-12 09:17:18 0 举报
AI智能生成
Vue源码分析
作者其他创作
大纲/内容
Vue
util
warn
extend
mergeOptions
defineReactive
set
delete
observable
options
directives
components
KeepAlive
filters
_base:Vue
use
mixin
extend
directive
component
filter
super
superOptions
extendOptions
sealedOptions
Vue.property
$isServer
$ssrContext
version
_init
$data
return this._data
$props
return this._props
$set
$delete
$watch
$on
$once
$off
$emit
_update
$forceUpdate
$destroy
$nextTick
_render
vm
_uid
_isVue
$options
经过编译添加render
_self
$parent
$children
$root
$refs
_watcher
_inactive
_directInactive
_isMounted
_isDestroyed
_isBeingDestroyed
_events
_vnode
$slots
$scopedSlots
_c
$createElement
$attrs
$listeners
_props
_data
_computedWatchers
_watcher
_watchers
_provided
$node
Watcher
vm
deep
user
lazy
sync
before
cb
dirty
deps
newDeps
depIds
newDepIds
Vnode
Dep
$mount
先找render
再找template
template是html的模版
template写的是#id
template写的是获取好的某个节点:document.querySelector
最后才找el
给options添加render
const {render, staticRenderFns} = compileToFunctions(template,options)
const {compile, compileToFunctions}=createCompiler(baseOptions)
createCompiler = createCompilerCreator(function baseCompile(template,options){})
function createCompilerCreator(baseCompile){
return function createCompiler(baseOptions){
function compile(template,options){
// 合并options
const finalOptions = Object.create(baseOptions)
finalOptions.modules = baseOptions.modules.concat(options.modules)
finalOptions.directives = extend(Object.create(baseOptions.directives, options.directives))
for(const key in options){
key !== 'modules' && key !== "directives" && (finalOptions[key]=options[key])
}
const compiled = baseCompile(template.trim(),finalOptions)
}
return {compile, compileToFunctions: createCompileToFunctionFn(compile)}
}
}
return function createCompiler(baseOptions){
function compile(template,options){
// 合并options
const finalOptions = Object.create(baseOptions)
finalOptions.modules = baseOptions.modules.concat(options.modules)
finalOptions.directives = extend(Object.create(baseOptions.directives, options.directives))
for(const key in options){
key !== 'modules' && key !== "directives" && (finalOptions[key]=options[key])
}
const compiled = baseCompile(template.trim(),finalOptions)
}
return {compile, compileToFunctions: createCompileToFunctionFn(compile)}
}
}
function createCompileToFunctionFn(compile){
return function compileToFunctions(template,options,vm){
const compiled = compile(template,options)
return compiled
}
}
return function compileToFunctions(template,options,vm){
const compiled = compile(template,options)
return compiled
}
}
baseOptions
expectHTML:true
modules
klass
staticKeys:['staticClass']
transformNode
genData
style
staticKeys:['staticStyle']
transformNode
genData
model
preTransformNode
directives
model
text
html
isPreTag
tag==='pre'
isUnaryTag
可以单闭合标签
mustUseProp
canBeLeftOpenTag
isReservedTag
html相关
svg相关
getTagNamespace
staticKeys:genStaticKeys(modules)
把modlules中的staticKeys包含在一块儿
编译
parse
parseHTML
匹配开始标签:处理属性
element=createASTElement(tag,attrs,currentParent)
{
type:1,
tag,
parent,
children:[],
attrList:[{name:'',value:''}...],
attrMap:{name:value},
rawAttrsMap:{name:{name:'',value:''},....}
}
type:1,
tag,
parent,
children:[],
attrList:[{name:'',value:''}...],
attrMap:{name:value},
rawAttrsMap:{name:{name:'',value:''},....}
}
processFor
形成对象:res: {
for: List,
alias: item || value,
itorator1: index || key,
itorator2: undefined || index
}
for: List,
alias: item || value,
itorator1: index || key,
itorator2: undefined || index
}
对象res都会通过extend的形式添加到el上
processIf
if exp : el.ifConditions:[{exp:exp,block:el}]
v-else: el.else = true
v-else-if: el.elseif = elseif-exp
processOnce
v-once: el.once=true
processElement
processRef
el.ref=refexp
el.refInFor
循环检查父元素在不在for里,有一个在即在
processSlotContent
<template scope="xxx">, <div slot-scope="xxx">
el.slotScope=exp
el.slotScope=exp
el.slotTarget
el.slotTargetDynamic
el.slotTargetDynamic
processSlotOutlet
el.slotName=exp
processComponent
el.is = exp
el.inlineTemplate
processAttrs
el.events:{name:[xx,xx,xx...]}
el.directives:{name:{}}
el.attrs:[key:{},key:{},...]
匹配结束标签:处理父子关系,v-if等关系
optimize
generate
genstatic
_m
genOnce
_o
genIf
(exp){return}
genChildren
genSlot
_t(slotName,children)
genComponent
_c(componentName,data,children)
genData
data:{
key: xx,
tag:xx,
attrs:[],
on:[xx],
nativeOn:[xx],
scopedSlots: _u
}
key: xx,
tag:xx,
attrs:[],
on:[xx],
nativeOn:[xx],
scopedSlots: _u
}
0 条评论
下一页