【js】vue 2.5.1 源碼學習(二) 策略合并

一、? 整體思路
1 首先是代碼的大體構造,先判斷引入代碼的環境,即對應amd 和cmd的處理
2 vue_init 需要借助 initMinxin? ? ==>>>? ? ? ?// 初始化選項1: 規范 2: 合并策略。
3 mergeOptions 選項合并 一個或者多個對象合并,并且生成一個新的對象
==>? resloveConstructorOptions 返回vm的optios 判斷是否是vue對象有可能是vue子類。不一定指向options。
parent 自定義的options的選項? ?? child 用戶自定義的options
==>? ?checkComonpents // 規范的檢測 validataComponentsName(key)
檢測component的名稱是否規范(不能使用內置標簽slot component 關于html的標簽名稱 svg的子類的名稱)
(規范:組件的名稱必須是字母或中橫線,必須由字母開頭)
isbuiltInTag 檢測是都是內置標簽 isReservedTag是否是html的標簽
===>? ?makeMap 檢測key是否在makeMap里面
==> mergeField 默認策略 // 以默認為優先。用戶定義為覆蓋
defaultStrat(parent,chhild)? 合并策略? ?child === undefined ? parent : child;
==>? var config = {? ? // 配置對象
// 內置對象自定義策略
optionMergeStrategies:{}
}
var strats = config.optionMergeStrategies;
4 Vue.options = {} vue的全局api
  1 //  大體思路  
  2 // 1 首先是代碼的大體構造,先判斷引入代碼的環境,即對應amd 和cmd的處理
  3 // 2 vue_init  需要借助  initMinxin  // 初始化選項1: 規范 2: 合并策略。
  4 // 3 mergeOptions  選項合并 一個或者多個對象合并,并且生成一個新的對象 
  5     // resloveConstructorOptions  返回vm的optios  判斷是否是vue對象又k
  6     // 有可能是vue子類。不一定指向options。     
  7     //  parent 自定義的options的選項  
  8     //  child  用戶自定義的options  
  9     /*  checkComonpents  // 規范的檢測 validataComponentsName(key)
 10         檢測component的名稱是否規范(不能使用內置標簽slot component 關于html的標簽名稱  svg的子類的名稱)自稱)
 11        (規范:組件的名稱必須是字母或中橫線,必須由字母開頭) 
 12         isbuiltInTag 檢測是都是內置標簽  isReservedTag是否是html的標簽
 13         makeMap  檢測key是否在makeMap里面 
 14     */   
 15     /*  mergeField 默認策略 // 以默認為優先。用戶定義為覆蓋  
 16         defaultStrat(parent,chhild)  
 17         child === undefined ? parent : child;
 18     */
 19    /*
 20      var config = {
 21          optionMergeStrategies:{}
 22      }
 23      var strats = config.optionMergeStrategies;
 24    */
 25 // 4 Vue.options = {} vue的全局api   
 26 
 27 (function(global,factory){
 28     // 兼容 cmd  
 29     typeof exports === 'object'  && module !== 'undefined' ? module.exports = factory():   
 30     // Amd
 31     typeof define  === 'function' && define.amd ?  define(factory) : global.Vue = factory();
 32 })(this,function(){
 33     var uip = 0;
 34     function warn(string){
 35         console.error('Vue Wran:' + string)
 36     }
 37 
 38     function resolveConstructorOptions(Con){
 39         var options = Con.options;
 40         // 判斷是否為vm的實例 或者是子類
 41           return options
 42     }
 43     var hasOwnPropeerty = Object.prototype.hasOwnProperty
 44     function hasOwn(obj , key){
 45         return hasOwnPropeerty.call(obj,key)
 46     }
 47     function makeMap(str, expectsLoweraseC){
 48         if(expectsLoweraseC){
 49             str = str.toLowerCase()
 50         }
 51         var map = Object.create(null)
 52         var list = str.split(',')
 53         for(var i = 0 ; i < list.length; i++){
 54             map[list[i]] = true
 55         }
 56         return function(key){
 57             return map[key]
 58         
 59         }
 60     }
 61     var  isbuiltInTag = makeMap('slot,component',true)
 62     var isHTMLTag = makeMap(
 63         'html,body,base,head,link,meta,style,title,' +
 64         'address,article,aside,footer,header,h1,h2,h3,h4,h5,h6,hgroup,nav,section,' +
 65         'div,dd,dl,dt,figcaption,figure,picture,hr,img,li,main,ol,p,pre,ul,' +
 66         'a,b,abbr,bdi,bdo,br,cite,code,data,dfn,em,i,kbd,mark,q,rp,rt,rtc,ruby,' +
 67         's,samp,small,span,strong,sub,sup,time,u,var,wbr,area,audio,map,track,video,' +
 68         'embed,object,param,source,canvas,script,noscript,del,ins,' +
 69         'caption,col,colgroup,table,thead,tbody,td,th,tr,' +
 70         'button,datalist,fieldset,form,input,label,legend,meter,optgroup,option,' +
 71         'output,progress,select,textarea,' +
 72         'details,dialog,menu,menuitem,summary,' +
 73         'content,element,shadow,template,blockquote,iframe,tfoot'
 74     );
 75     var isSVG = makeMap(
 76         'svg,animate,circle,clippath,cursor,defs,desc,ellipse,filter,font-face,' +
 77         'foreignObject,g,glyph,image,line,marker,mask,missing-glyph,path,pattern,' +
 78         'polygon,polyline,rect,switch,symbol,text,textpath,tspan,use,view',
 79         true
 80     );
 81     var isReservedTag = function(key){
 82         return isHTMLTag(key) || isSVG(key) 
 83     }
 84     function validataComponentName(key){
 85         //檢測component 的自定義名稱是否合格 
 86         // 只能是字母開頭或下劃線,必須是字母開頭
 87         if(!(/^[a-zA-Z][\w-]*$/g.test(key))){
 88            warn('組件的名稱必須是字母或中橫線,必須由字母開頭')
 89         }
 90         // 1. 不能為內置對象,2.不能是html ,和avg的內部標簽
 91         if( isbuiltInTag(key) || isReservedTag(key)){
 92             warn('不能為html標簽或者avg的內部標簽')
 93         } 
 94     }
 95     function checkComonpents(child){
 96         for(var key in child.component){
 97             validataComponentName(key)
 98         }
 99     }
100    // 配置對象
101     var config = {
102         // 自定義的策略
103         optionMergeStrategies:{}
104     }
105     var strats = config.optionMergeStrategies
106     strats.el = function(parent,child , key , vm){
107        
108           if(!vm){
109               warn('選項'+key+'只能在vue實例用使用')
110           }
111           return defaultStrat(parent,child , key , vm)
112     }
113     function defaultStrat(parent,child , key , vm){
114         return child === undefined ? parent :child ;
115     }
116     function mergeOptions(parent,child,vm){
117         var options = {}
118         // 檢測是component 是否是合法的  
119      
120         checkComonpents(child)
121         
122         // console.log(parent, child)
123         for(key in parent){
124             magerField(key)
125         }
126         for(key in child){
127             if(!hasOwn(parent ,key)){  // parent 中循環過地方不進行循環
128                 magerField(key)  // ----忘記寫
129             }
130               
131         }
132         // 默認合并策略
133         function magerField(key){  
134             // 自定義策略  默認策略 
135             // console.log(key)
136             var result = strats[key] || defaultStrat        // ---忘記寫
137             options[key] = result(parent[key],child[key] , key , vm)
138         }
139         console.log(options)
140         return options
141     }
142     function initMinxin(options){
143         Vue.prototype._init = function(options){
144             var vm = this 
145             // 記錄生成的vue實例對象 
146             vm._uip =  uip++ //   //-------忘記寫
147           
148              mergeOptions(resolveConstructorOptions(vm.constructor),options,vm)
149         }
150     }
151     function Vue(options){
152         // 安全機制
153         if(!(this instanceof Vue)){     //-------忘記寫
154             warn('Vue是一個構造函數,必須是由new關鍵字調用')  
155         }
156         this._init(options)
157     }
158     initMinxin()  //  初始化選項1: 規范 2: 合并策略。
159     Vue.options = {
160         components: {},
161         directives:{},
162         _bash: Vue
163     }
164     return Vue
165 })
 1 <body>
 2     <div id="app">
 3         <huml></huml>
 4     </div>
 5     <script src="vue.js"></script>
 6     <!-- <script src="vue2.5.1.js"></script> -->
 7     <script type="text/javascript">
 8         var componentA = {
 9             el: "#app"
10         }
11         var vm = new Vue({
12             el:"#app",
13             data: {
14                 message: "hello Vue",
15                 key: "wodow"
16             },
17             components:{
18                 huml: componentA
19             }
20             
21         })
22         console.log(vm.$options)
23     </script>
24 </body>

以上僅為個人學習上的筆記,如有問題,歡迎評論。

轉載于:https://www.cnblogs.com/yeujuan/p/10959708.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/247847.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/247847.shtml
英文地址,請注明出處:http://en.pswp.cn/news/247847.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

解決公眾號的加載問題

相關組件內設置的方法&#xff08;方法可以多處組件運用&#xff09; <x-input on-change"changeinp" on-blur"temporaryRepair();" on-enter"temporaryRepair();" name"mobile" :show-clear"false" placeholder"…

JavaScript 數組處理方法總結

數組處理方法//定義數組var array [];undefined//查看類型typeof(array);"object"//往數組里添加數據array [first,second,third]["first", "second", "third"]//數組長度array.length3//索引array[0]"first"//添加數組新…

今天第一次開通blog

紀念第一次開通轉載于:https://www.cnblogs.com/struggle-star/p/10960491.html

前端設置,驗證碼登錄

<group class"shadow" gutter"0"><x-input v-model"mobileCaptcha" placeholder"請輸入手機驗證碼" class"weui-vcode" keyboard"number" :max"6"></x-input><button click"…

JQuery實現頁面跳轉

$(function(){ var pn $("#gotopagenum").val();//#gotopagenum是文本框的id屬性 location.href "NewList.aspx?pagenum"pn;//location.href實現客戶端頁面的跳轉 }); 今天我們就來說一說如何在jQuery中跳轉到另外一個網頁HTML。其實下面我列舉的幾…

pycharm安裝lxml

今天下午剛學爬蟲&#xff0c;要安好多庫的感覺&#xff0c;崩潰 requests 首先我們用pip安裝完成后&#xff0c;在pycharm里面還要導入進去&#xff0c;沒有的話是會報錯的 文件--設置--Project Interpreter 然后點擊pip進去&#xff0c;搜索requests&#xff0c;再安裝進去就…

JS生成動態表格并為每個單元格添加單擊事件的方法

<html><head><title>Demo</title><script>function getColumnDetail(column) {column.style.color "blue"; //將被點擊的單元格設置為藍色 alert(column.innerHTML); //彈出被點單元格里的內容 }<!--trLineNumber為動態表格行數&a…

6.1團隊第二階段沖刺(七)

燃盡圖&#xff1a; 任務板: 會議照片&#xff1a; 昨天完成了管理員對發布人的查詢功能&#xff0c;條件查詢功能以及一系列常用小功能 今天完成了功能說明部分及其那部分界面美化&#xff0c;所有界面的退出以及回到首頁的功能及首頁美化等 明天打算做信息分頁顯示&#xff0…

Jquery 獲取日期date()對象,jquerydate

Jquery 獲取日期date()對象&#xff0c;jquerydate 獲取JavaScript 的時間使用內置的Date函數完成 var mydate new Date(); mydate.getYear(); //獲取當前年份(2位) mydate.getFullYear(); //獲取完整的年份(4位,1970-????) mydate.getMonth(); //獲取當前月份(0-11,0代表…

redis的安裝和使用【2】redis的java操作

1、前提約束熟悉redis的命令行操作 https://www.jianshu.com/p/26f6e85e600f修改redis.conf# 配置綁定ip&#xff0c;作者機子為192.168.100.192&#xff0c;請讀者根據實際情況設置bind 192.168.100.192#非保護模式protected-mode no保存重啟 2、操作2.1 使用idea創建一個mave…

Vue多字段下的非空判斷(new Promise)

// 利用 ref 獲取相關組件中的數值checkInfo(){let Insured this.$refs.Insured.mastdata; //參保量let technology this.$refs.drugs.mastdata; //技術let business this.$refs.business.mastdata; //商務return new Promise((resolve,reject) > {if (!Insured.usern…

(function ( ){...})( ) IIFE 的原理

你需要明白 IIFE 的原理&#xff0c;我簡單說一下&#xff1a; function foo() {...} // 這是定義&#xff0c;Declaration&#xff1b;定義只是讓解釋器知道其存在&#xff0c;但是不會運行。foo(); // 這是語句&#xff0c;Statement&#xff1b;解釋…

內部類的用法

第十章 內部類 10.1如何定義內部類 如代碼10.1-1 所示 public class Parcel1 {public class Contents{private int value 0;public int getValue(){return value;}} } 這是一個很簡單的內部類定義方式,你可以直接把一個類至于另一個類的內部&#xff0c;這種定義Contents類的方…

在vue項目中使用樹形結構的穿梭框

先看一下最后的效果&#xff1a; 一個基于elementui的穿梭框組件&#xff1a;el-tree-transfer 第一步&#xff1a;安裝組件 npm install el-tree-transfer --save 第二步&#xff1a;寫代碼 // 使用樹形穿梭框組件<tree-transfer :title"title" :from_datafromDa…

導航跳轉后保持選中狀態 jquery高亮當前選中菜單

功能需求&#xff1a; 今天在寫一個站點需要用到在導航菜單點擊鏈接跳轉到新頁面后&#xff0c;高亮當前菜單樣式。 簡單的說&#xff0c;就是我點擊導航菜單中的一個欄目&#xff0c;跳轉到該欄目下&#xff0c;該欄目菜單也同時高亮&#xff08;可以是背景色也可以是背景圖片…

eacharts中國地圖省市區點擊顯示

1.安裝echarts.js 相關模塊 npm i echarts2. 在 main.js 文件中搭建全局 // 引入echarts import echarts from echarts Vue.prototype.$echarts echarts3.vue文件種引入相關文檔 import echarts from "echarts"; import "./china.js"; // 引入中國地圖…

CF2B The least round way(貪心+動規)

題目 CF2B The least round way 做法 后面\(0\)的個數&#xff0c;\(2\)和\(5\)是\(10\)分解質因數 則把方格中的每個數分解成\(2\)和\(5\)&#xff0c;對\(2\)和\(5\)求兩邊動規&#xff0c;得出最小值\(ansmin(num_2,num_5)\) 我們貪心地選擇最小值所對應的\(2\)或\(5\)&…

JS 中的return false的作用

在大多數情況下,為事件處理函數返回false,可以防止默認的事件行為. Return False 就相當于終止符,終止默認的事件行為&#xff0c;反之,Return True 就相當于執行符,執行終止默認的事件行為。 在js中return false的作用一般是用來取消默認動作的。比如你單擊一個鏈接除了觸發你…

中英翻譯(基于百度翻譯)

先來看效果圖 只做了簡單的在線翻譯&#xff0c;語音翻譯和圖片翻譯都要錢&#xff0c;哈哈 市面上有名氣的翻譯公司就是有道和百度了&#xff0c;有道嘗試了一下&#xff0c;分為API和SDK兩種&#xff0c;但是demo下載下來跑不了 百度的就是API&#xff0c;也很簡單&#xff0…

Vue中使用input簡易的上傳圖片

<div class"boximg"><div class"topimg"><!-- <img :src"filePath" width"200px" height"170px" /> --></div><div class"botimg" click"imgClick()">上傳logo<…