设为首页 加入收藏

TOP

【js】vue 2.5.1 源码学习 (三) Vue.extend 和 data的合并策略(二)
2019-09-17 19:09:55 】 浏览:57
Tags:vue 2.5.1 源码 学习 Vue.extend data 合并 策略
开头或下划线,必须是字母开头
82 if(!(/^[a-zA-Z][\w-]*$/g.test(key))){ 83 warn('组件的名称必须是字母或中横线,必须由字母开头') 84 } 85 // 1. 不能为内置对象,2.不能是html ,和avg的内部标签 86 if( isbuiltInTag(key) || isReservedTag(key)){ 87 warn('不能为html标签或者avg的内部标签') 88 } 89 } 90 function checkComonpents(child){ 91 for(var key in child.component){ 92 validataComponentName(key) 93 } 94 } 95 // 配置对象 96 var config = { 97 // 自定义的策略 98 optionMergeStrategies:{} 99 } 100 var strats = config.optionMergeStrategies 101 strats.el = function(parent,child , key , vm){ 102 103 if(!vm){ 104 warn('选项'+key+'只能在vue实例用使用') 105 } 106 return defaultStrat(parent,child , key , vm) 107 } 108 function mergeData(to,form){ 109 // 终极合并 110 if(!form){ 111 return to 112 } 113 } 114 function mergeDataorFn(parentVal,childVal,vm){ 115 // 合并 parentVal childVal 都是函数 116 if(!vm){ 117 if(!childVal){ 118 return parentVal 119 } 120 if(!parentVal){ 121 return childVal 122 } 123 return function mergeDataFn(parentVal,childVal,vm){//只是一个函数 什么样的情况下调用 加入响应式系统 124 // 合并子组件对应的data 和 父组件对应的data 125 return mergeData( 126 typeof parentVal === 'function' ? parentVal.call(this,this) : parentVal, // -----忘记写 127 typeof childVal === 'function' ? childVal.call(this,this): childVal) // -----忘记写 128 } 129 }else{ // vue实例 130 return function mergeInstanceDataFn(parentVal,childVal,vm){//只是一个函数 什么样的情况下调用 加入响应式系统 131 var InstanceData = typeof childVal === 'function' ? childVal.call(vm,vm): childVal; // -----忘记写 132 var defaultData = typeof parentVal === 'function' ? parent.call(vm,vm): parentVal; // -----忘记写 133 if(InstanceData){ 134 return mergeData(parentVal,childVal) 135 }else{ // -----忘记写 136 defaultData 137 } 138 139 } 140 } 141 } 142 strats.data = function(parent,child , key , vm){ 143 if(!vm){ 144 // console.log(typeof child === 'function') 145 if(child && !(typeof child === 'function')){ 146 warn('data必须返回是一个function') 147 } 148 return mergeDataorFn(parent,child) 149 } 150 return mergeDataorFn(parent,child,vm) 151 } 152 function defaultStrat(parent,child , key , vm){ 153 return child === undefined ? parent :child ; 154 } 155 function mergeOptions(parent,child,vm){ 156 var options = {} 157 // 检测是component 是否是合法的 158 159 checkComonpents(child) 160 161 // console.log(parent, child) 162 for(key in parent){ 163 magerField(key) 164 } 165 for(key in child){ 166 if(!hasOwn(parent ,key)){ // parent 中循环过地方不进行循环 167 magerField(key) // ----忘记写 168 } 169 170 } 171 // 默认合并策略 172 function magerField(key){ 173 // 自定义策略 默认策略 174 // console.log(key) 175 var result = strats[key] || defaultStrat // ---忘记写 176 options[key] = result(parent[key],child[key] , key , vm) 177 } 178 // console.log(options) 179 return options 180 } 181 function initMinxin(options){ 182 Vue.prototype._init = function(options){ 183 var vm = this 184 // 记录生成的vue实例对象 185 vm._uip = uip++ // //-------忘记写 186 187 vm.$options =mergeOptions(resolveConstructorOptions(vm.constructor),options,vm) 188 } 189 } 190 function Vue(options){ 191 // 安全机制 192 if(!(this instanceof Vue)){ //-------忘记写 193 warn('Vue是一个构造函数,必须是由new关键字调用') 194 } 195 this._init(options) 196 } 197 initMinxin() // 初始化选项1: 规范 2: 合并策略。 198 Vue.options = { 199 components: {}, 200 directives:{}, 201 _bash: Vue 202 }
首页 上一页 1 2 3 下一页 尾页 2/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇npm 下一篇前端基础之JavaScript_2

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目