国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > JavaScript > 正文

Vue如何實現(xiàn)組件的源碼解析

2024-05-06 15:16:09
字體:
供稿:網(wǎng)友

官網(wǎng)上關(guān)于組件繼承分為兩大類,全局組件和局部組件。無論哪種方式,最核心的是創(chuàng)建組件,然后根據(jù)場景不同注冊組件。

有一點要牢記,“Vue.js 組件其實都是被擴展的 Vue 實例”!

1. 全局組件

// 方式一var MyComponent = Vue.extend({  name: 'my-component',  template: '<div>A custom component!</div>'});Vue.component('my-component', MyComponent);// 方式二Vue.component('my-component', {  name: 'my-component',  template: '<div>A custom component!</div>'});// 使用組件<div id="example">  <my-component></my-component></div>

主要涉及到兩個靜態(tài)方法:

    Vue.extend:通過擴展Vue實例的方法創(chuàng)建組件 Vue.component:注冊組件

先來看看Vue.extend源碼,解釋參考中文注釋:

Vue.extend = function (extendOptions) { extendOptions = extendOptions || {}; var Super = this; var isFirstExtend = Super.cid === 0; if (isFirstExtend && extendOptions._Ctor) {  return extendOptions._Ctor; } var name = extendOptions.name || Super.options.name; // 如果有name屬性,即組件名稱,檢測name拼寫是否合法 if ('development' !== 'production') {  if (!/^[a-zA-Z][/w-]*$/.test(name)) {   warn('Invalid component name: "' + name + '". Component names ' + 'can only contain alphanumeric characaters and the hyphen.');   name = null;  } } // 創(chuàng)建一個VueComponent 構(gòu)造函數(shù),函數(shù)名為‘VueComponent'或者name var Sub = createClass(name || 'VueComponent'); // 構(gòu)造函數(shù)原型繼承Vue.prototype Sub.prototype = Object.create(Super.prototype); Sub.prototype.constructor = Sub; Sub.cid = cid++; // 合并Vue.options和extendOptions,作為新構(gòu)造函數(shù)的靜態(tài)屬性options   Sub.options = mergeOptions(Super.options, extendOptions); //'super'靜態(tài)屬性指向Vue函數(shù) Sub['super'] = Super; // start-----------------拷貝Vue靜態(tài)方法   // allow further extension Sub.extend = Super.extend; // create asset registers, so extended classes // can have their private assets too. config._assetTypes.forEach(function (type) {  Sub[type] = Super[type]; }); // end-----------------拷貝Vue靜態(tài)方法   // enable recursive self-lookup if (name) {  Sub.options.components[name] = Sub; } // cache constructor:緩存該構(gòu)造函數(shù) if (isFirstExtend) {  extendOptions._Ctor = Sub; } return Sub;};

可以看到,Vue.extend的關(guān)鍵點在于:創(chuàng)建一個構(gòu)造函數(shù)function VueComponent(options) { this._init(options) },通過原型鏈繼承Vue原型上的屬性和方法,再講Vue的靜態(tài)函數(shù)賦值給該構(gòu)造函數(shù)。

再看看Vue.component源碼,解釋參考中文注釋:

// _assetTypes: ['component', 'directive', 'elementDirective', 'filter', 'transition', 'partial']config._assetTypes.forEach(function (type) { // 靜態(tài)方法Vue.component Vue[type] = function (id, definition) {  if (!definition) {   return this.options[type + 's'][id];  } else {   /* istanbul ignore if */   if ('development' !== 'production') {    if (type === 'component' && (commonTagRE.test(id) || reservedTagRE.test(id))) {     warn('Do not use built-in or reserved HTML elements as component ' + 'id: ' + id);    }   }   // 如果第二個參數(shù)是簡單對象,則需要通過Vue.extend創(chuàng)建組件構(gòu)造函數(shù)   if (type === 'component' && isPlainObject(definition)) {    if (!definition.name) {     definition.name = id;    }    definition = Vue.extend(definition);   }   // 將組件函數(shù)加入Vue靜態(tài)屬性options.components中,也就是,全局注入該組件   this.options[type + 's'][id] = definition;   return definition;  } };});            
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表

圖片精選

主站蜘蛛池模板: 璧山县| 东港市| 灵山县| 自贡市| 沿河| 墨竹工卡县| 进贤县| 南开区| 鄱阳县| 吴旗县| 赤壁市| 错那县| 布尔津县| 登封市| 涡阳县| 祁东县| 分宜县| 大名县| 腾冲县| 汤阴县| 平乡县| 泰顺县| 和田县| 涿州市| 闵行区| 恩平市| 南川市| 宾川县| 平凉市| 兴山县| 涡阳县| 黄浦区| 景泰县| 张家口市| 大悟县| 如东县| 普陀区| 嘉祥县| 巴塘县| 湾仔区| 无极县|