vue 2.5.1 源碼學(xué)習(xí) 之Vue.extend 和 data的合并策略
1. 子類父類
2.Vue.extend() //創(chuàng)建vue的子類
組件的語法器 Vue.extend(options)
Profile().$mount('#app')
// 掛在app上,并替換app
新建 initExend
==》 Vue.extend
3. strat.data
==> if(!vm){子組件中data的值是一個(gè)方法function ==> mergeDataorFn()} // 數(shù)據(jù)的合并
==> else {} //通過實(shí)例綁定的data 實(shí)際是一個(gè)函數(shù) mergeDataorFn
==》 mergeDataorFn if(!vm) mergeDataFn ==> mergeData()
else ==》mergedInstanceDataFn ==>mergeData()
mergeData(to,from) //終極合并
jquery.extend // 深copy和淺copy
// 大體思路 (二) // 1. 子類父類 /* 2.Vue.extend() //創(chuàng)建vue的子類 組件的語法器 Vue.extend(options) Profile().$mount('#app') // 掛在app上,并替換app 新建 initExend ==》 Vue.extend */ /* 3. strat.data ==> if(!vm){子組件中data的值是一個(gè)方法function ==> mergeDataorFn()} // 數(shù)據(jù)的合并 ==> else {} //通過實(shí)例綁定的data 實(shí)際是一個(gè)函數(shù) mergeDataorFn ==》 mergeDataorFn if(!vm) mergeDataFn ==> mergeData() else ==》mergedInstanceDataFn ==>mergeData() mergeData(to,from) //終極合并 jquery.extend // 深copy和淺copy */ (function(global,factory){ // 兼容 cmd typeof exports === 'object' && module !== 'undefined' ? module.exports = factory(): // Amd typeof define === 'function' && define.amd ? define(factory) : global.Vue = factory(); })(this,function(){ var uip = 0; function warn(string){ console.error('Vue Wran:' + string) } function resolveConstructorOptions(Con){ var options = Con.options; // 判斷是否為vm的實(shí)例 或者是子類 return options } var hasOwnPropeerty = Object.prototype.hasOwnProperty function hasOwn(obj , key){ return hasOwnPropeerty.call(obj,key) } function makeMap(str, expectsLoweraseC){ if(expectsLoweraseC){ str = str.toLowerCase() } var map = Object.create(null) var list = str.split(',') for(var i = 0 ; i < list.length; i++){ map[list[i]] = true } return function(key){ return map[key] } } var isbuiltInTag = makeMap('slot,component',true) var isHTMLTag = makeMap( 'html,body,base,head,link,meta,style,title,' + 'address,article,aside,footer,header,h1,h2,h3,h4,h5,h6,hgroup,nav,section,' + 'div,dd,dl,dt,figcaption,figure,picture,hr,img,li,main,ol,p,pre,ul,' + 'a,b,abbr,bdi,bdo,br,cite,code,data,dfn,em,i,kbd,mark,q,rp,rt,rtc,ruby,' + 's,samp,small,span,strong,sub,sup,time,u,var,wbr,area,audio,map,track,video,' + 'embed,object,param,source,canvas,script,noscript,del,ins,' + 'caption,col,colgroup,table,thead,tbody,td,th,tr,' + 'button,datalist,fieldset,form,input,label,legend,meter,optgroup,option,' + 'output,progress,select,textarea,' + 'details,dialog,menu,menuitem,summary,' + 'content,element,shadow,template,blockquote,iframe,tfoot' ); var isSVG = makeMap( 'svg,animate,circle,clippath,cursor,defs,desc,ellipse,filter,font-face,' + 'foreignObject,g,glyph,image,line,marker,mask,missing-glyph,path,pattern,' + 'polygon,polyline,rect,switch,symbol,text,textpath,tspan,use,view', true ); var isReservedTag = function(key){ return isHTMLTag(key) || isSVG(key) } function validataComponentName(key){ //檢測(cè)component 的自定義名稱是否合格 // 只能是字母開頭或下劃線,必須是字母開頭 if(!(/^[a-zA-Z][\w-]*$/g.test(key))){ warn('組件的名稱必須是字母或中橫線,必須由字母開頭') } // 1. 不能為內(nèi)置對(duì)象,2.不能是html ,和avg的內(nèi)部標(biāo)簽 if( isbuiltInTag(key) || isReservedTag(key)){ warn('不能為html標(biāo)簽或者avg的內(nèi)部標(biāo)簽') } } function checkComonpents(child){ for(var key in child.component){ validataComponentName(key) } } // 配置對(duì)象 var config = { // 自定義的策略 optionMergeStrategies:{} } var strats = config.optionMergeStrategies strats.el = function(parent,child , key , vm){ if(!vm){ warn('選項(xiàng)'+key+'只能在vue實(shí)例用使用') } return defaultStrat(parent,child , key , vm) } function mergeData(to,form){ // 終極合并 if(!form){ return to } } function mergeDataorFn(parentVal,childVal,vm){ // 合并 parentVal childVal 都是函數(shù) if(!vm){ if(!childVal){ return parentVal } if(!parentVal){ return childVal } return function mergeDataFn(parentVal,childVal,vm){//只是一個(gè)函數(shù) 什么樣的情況下調(diào)用 加入響應(yīng)式系統(tǒng) // 合并子組件對(duì)應(yīng)的data 和 父組件對(duì)應(yīng)的data return mergeData( typeof parentVal === 'function' ? parentVal.call(this,this) : parentVal, // -----忘記寫 typeof childVal === 'function' ? childVal.call(this,this): childVal) // -----忘記寫 } }else{ // vue實(shí)例 return function mergeInstanceDataFn(parentVal,childVal,vm){//只是一個(gè)函數(shù) 什么樣的情況下調(diào)用 加入響應(yīng)式系統(tǒng) var InstanceData = typeof childVal === 'function' ? childVal.call(vm,vm): childVal; // -----忘記寫 var defaultData = typeof parentVal === 'function' ? parent.call(vm,vm): parentVal; // -----忘記寫 if(InstanceData){ return mergeData(parentVal,childVal) }else{ // -----忘記寫 defaultData } } } } strats.data = function(parent,child , key , vm){ if(!vm){ // console.log(typeof child === 'function') if(child && !(typeof child === 'function')){ warn('data必須返回是一個(gè)function') } return mergeDataorFn(parent,child) } return mergeDataorFn(parent,child,vm) } function defaultStrat(parent,child , key , vm){ return child === undefined ? parent :child ; } function mergeOptions(parent,child,vm){ var options = {} // 檢測(cè)是component 是否是合法的 checkComonpents(child) // console.log(parent, child) for(key in parent){ magerField(key) } for(key in child){ if(!hasOwn(parent ,key)){ // parent 中循環(huán)過地方不進(jìn)行循環(huán) magerField(key) // ----忘記寫 } } // 默認(rèn)合并策略 function magerField(key){ // 自定義策略 默認(rèn)策略 // console.log(key) var result = strats[key] || defaultStrat // ---忘記寫 options[key] = result(parent[key],child[key] , key , vm) } // console.log(options) return options } function initMinxin(options){ Vue.prototype._init = function(options){ var vm = this // 記錄生成的vue實(shí)例對(duì)象 vm._uip = uip++ // //-------忘記寫 vm.$options =mergeOptions(resolveConstructorOptions(vm.constructor),options,vm) } } function Vue(options){ // 安全機(jī)制 if(!(this instanceof Vue)){ //-------忘記寫 warn('Vue是一個(gè)構(gòu)造函數(shù),必須是由new關(guān)鍵字調(diào)用') } this._init(options) } initMinxin() // 初始化選項(xiàng)1: 規(guī)范 2: 合并策略。 Vue.options = { components: {}, directives:{}, _bash: Vue } function initExend(Vue){ Vue.extend = function(extendOptions){ extendOptions = extendOptions || {} // -----忘記寫 var Super = this var Child = function VueComponent() { this._init(options) } Child.prototype = Object.create(Super.prototype) Child.prototype.constructor = Child // 改變constructor 的指向 Child.options = mergeOptions(Super.options,extendOptions) // 子類繼承父類的靜態(tài)方法。 Child.extend = Vue.extend return Child } } initExend(Vue) return Vue }) <body> <div id="app"> <huml></huml> </div> <script src="vue.js"></script> <!-- <script src="vue2.5.1.js"></script> --> <script type="text/javascript"> var componentA = { el: "#app" } var vm = new Vue({ el:"#app", data: { message: "hello Vue", key: "wodow" }, components:{ huml: componentA } }) // console.log(Vue) var Parent = Vue.extend({ data: function() {} }) var Child = Parent.extend({}); console.log(vm.$options) </script> </body>
總結(jié)
以上所述是小編給大家介紹的vue 2.5.1 源碼學(xué)習(xí) 之Vue.extend 和 data的合并策略,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
如果你覺得本文對(duì)你有幫助,歡迎轉(zhuǎn)載,煩請(qǐng)注明出處,謝謝!
相關(guān)文章
在小程序/mpvue中使用flyio發(fā)起網(wǎng)絡(luò)請(qǐng)求的方法
這篇文章主要介紹了在小程序/mpvue中使用flyio發(fā)起網(wǎng)絡(luò)請(qǐng)求的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-09-09vue如何實(shí)現(xiàn)簡(jiǎn)易的彈出框
這篇文章主要介紹了vue如何實(shí)現(xiàn)簡(jiǎn)易的彈出框,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-04-04vue動(dòng)態(tài)加載SVG文件并修改節(jié)點(diǎn)數(shù)據(jù)的操作代碼
這篇文章主要介紹了vue動(dòng)態(tài)加載SVG文件并修改節(jié)點(diǎn)數(shù)據(jù)的方法,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-08-08vue中的el-tree @node-click傳自定義參數(shù)
這篇文章主要介紹了vue中的el-tree @node-click傳自定義參數(shù)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08vue中@click和@click.native.prevent的區(qū)別
這篇文章主要介紹了vue中@click和@click.native.prevent的區(qū)別,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-04-04vue+element?ui表格添加多個(gè)搜索條件篩選功能(前端查詢)
這篇文章主要給大家介紹了關(guān)于vue+element?ui表格添加多個(gè)搜索條件篩選功能的相關(guān)資料,最近在使用element-ui的表格組件時(shí),遇到了搜索框功能的實(shí)現(xiàn)問題,需要的朋友可以參考下2023-08-08vue組件傳值的實(shí)現(xiàn)方式小結(jié)【三種方式】
這篇文章主要介紹了vue組件傳值的實(shí)現(xiàn)方式,結(jié)合實(shí)例形式總結(jié)分析了vue.js組建傳值的三種實(shí)現(xiàn)方式,包括父?jìng)髯?、子傳父及非父子傳?需要的朋友可以參考下2020-02-02