Skip to content

Commit d087f36

Browse files
committed
_init
1 parent 87d5cbf commit d087f36

File tree

2 files changed

+322
-0
lines changed

2 files changed

+322
-0
lines changed

123.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# title

_init.md

Lines changed: 321 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,19 +324,340 @@ function initExtend(Vue){
324324
extendOptions
325325
);
326326
Sub['super'] = Super;
327+
328+
//
329+
if(Sub.options.props){
330+
initProps$1(Sub);
331+
}
332+
if(Sub.options.computed){
333+
initComputed$1(Sub);
334+
}
335+
336+
// 扩展
337+
Sub.extend = Super.extend;
338+
Sub.mixin = Super.mixin;
339+
Sub.use = Super.use;
340+
341+
// 创建私有值
342+
config._assetTypes.forEach(function(type){
343+
Sub[type] = Super[type];
344+
});
345+
346+
// 自检查递归
347+
if(name){
348+
Sub.options.components[name] = Sub;
349+
}
350+
351+
//
352+
Sub.superOptions = Super.options;
353+
Sub.extendOptions = extendOptions;
354+
Sub.sealedOptions = extend({}, Sub.options);
355+
356+
// 缓存构造函数
357+
cachedCtors[SuperId] = Sub;
358+
return Sub;
359+
};
360+
}
361+
```
362+
363+
364+
365+
### initProps$1
366+
367+
```javascript
368+
function initProps$1(Comp){
369+
var props = Comp.options.props;
370+
for(var key in props){
371+
proxy(Comp.prototype, '_props_', key);
372+
}
373+
}
374+
```
375+
376+
377+
378+
### initComputed$1
379+
380+
```javascript
381+
function initComputed$1(Comp){
382+
var computed = Comp.options.computed;
383+
for(var key in computed){
384+
defineComputed(Comp.prototype, key, computed[key]);
327385
}
328386
}
329387
```
330388

331389

332390

391+
---
333392

334393

335394

395+
## initAssetRegisters
336396

397+
```javascript
398+
function initAssetRegisters(Vue){
399+
config._assetTypes.forEach(function(type){
400+
Vue[type] = function(id, definition){
401+
if(!definition){
402+
return this.options[type + 's'][id];
403+
} else{
404+
// 不要用内置或保留HTML标签做组件ID
405+
if(type === 'component' && config.isReservedTag(id)){
406+
warn(
407+
'Do not use bult-in or reserved HTML elements as component ' +
408+
'id: ' + id
409+
);
410+
}
411+
if(type === 'component' && isPlainObject(definition)){
412+
definition.name = definition.name || id;
413+
definition = this.options._base.extend(definition);
414+
}
415+
if(type === 'directive' && typeof definition === 'function'){
416+
definition = { bind: definition, update: definition };
417+
}
418+
this.options[type + 's'][id] = definition;
419+
return definition;
420+
}
421+
};
422+
});
423+
}
424+
```
425+
426+
427+
428+
---
337429

338430

339431

432+
## patternTypes
433+
434+
```javascript
435+
var patternTypes = [String, RegExp];
436+
```
437+
438+
439+
440+
---
441+
442+
443+
444+
## getComponentName
445+
446+
```javascript
447+
function getComponentName(opts){
448+
return opts && (opts.Ctor.options.name || opts.tag);
449+
}
450+
```
451+
452+
453+
454+
---
455+
456+
457+
458+
## matches
459+
460+
```javascript
461+
function matches(pattern, name){
462+
// 测试对应pattern是否包含name
463+
if(typeof pattern === 'string'){
464+
return pattern.split(',').indexOf(name) > -1;
465+
} else if(pattern instanceof RegExp){
466+
return pattern.test(name);
467+
}
468+
return false;
469+
}
470+
```
471+
472+
473+
474+
---
475+
476+
477+
478+
## pruneCache
479+
480+
```javascript
481+
function pruneCache(cache, filter){
482+
for(var key in cache){
483+
var cachedNode = cache[key];
484+
if(cachedNode){
485+
var name = getComponentName(cachedNode.componentOptions);
486+
if(name && !filter(name)){
487+
pruneCacheEntry(cachedNode);
488+
cache[key] = null;
489+
}
490+
}
491+
}
492+
}
493+
```
494+
495+
496+
497+
---
498+
499+
500+
501+
## pruneCacheEntry
502+
503+
```javascript
504+
function pruneCacheEntry(vnode){
505+
if(vnode){
506+
if(!vnode.componentInstance._inactive){
507+
callHook(vnode.componentInstance, 'deactivated');
508+
}
509+
vnode.componentInstance.$destroy();
510+
}
511+
}
512+
```
513+
514+
515+
516+
---
517+
518+
519+
520+
## KeepAlive
521+
522+
```javascript
523+
var KeepAlive = {
524+
name:'keep-alive',
525+
abstract:true,
526+
527+
props:{
528+
include: patternTypes,
529+
exclude: patternTypes
530+
},
531+
532+
created: function created(){
533+
this.cache = Object.create(null);
534+
},
535+
536+
destroyed: function destroyed(){
537+
var this$1 = this;
538+
for(var key in this$1.cache[key]){
539+
pruneCacheEntry(this$1.cache[key]);
540+
}
541+
},
542+
543+
watch: {
544+
include: function include(val){
545+
pruneCache(this.cache, function(name){ return matches(val, name); });
546+
},
547+
exclude: function exclude(val){
548+
pruneCache(this.cache, function(name){ return !matches(val, name); });
549+
}
550+
},
551+
552+
render: function render(){
553+
var vnode = getFirstComponentChild(this.$slot.default);
554+
var componentOptions = vnode && vnode.componentOptions;
555+
if(componentOptions){
556+
// 检查类型
557+
var name = getComponentName(componentOptions);
558+
if(name && (
559+
(this.include && !matches(this.include, name)) ||
560+
(this.exclude && matches(this.exclude, name))
561+
)){
562+
return vnode;
563+
}
564+
// 3种情况
565+
// 1.vnode.key
566+
// 2.componentOptions.Ctor.cid::componentOptions.tag
567+
// 3.componentOptions.Ctor.cid
568+
var key = vnode.key == null ?
569+
componentOptions.Ctor.cid + (componentOptions.tag ?
570+
('::' + (componentOptions.tag)) : '') : vnode.key;
571+
if(this.cache[key]){
572+
vnode.componentInstance = this.cache[key].componentInstance;
573+
} else{
574+
this.cache[key] = vnode;
575+
}
576+
vnode.data.keepAlive = true;
577+
}
578+
return vnode;
579+
}
580+
};
581+
```
582+
583+
584+
585+
---
586+
587+
588+
589+
## builtInComponents
590+
591+
```javascript
592+
var builtInComponents = {
593+
KeepAlive: KeepAlive
594+
};
595+
```
596+
597+
598+
599+
---
600+
601+
602+
603+
## initGlobalAPI
604+
605+
```javascript
606+
function initGlobalAPI(Vue){
607+
var configDef = {};
608+
configDef.get = function(){ return config; };
609+
// Vue.config对象不可更改
610+
configDef.set = function(){
611+
warn(
612+
'Do not replace the Vue.config object, set individual fields instead.'
613+
);
614+
};
615+
Object.defineProperty(Vue, 'config', configDef);
616+
617+
// 暴露通用方法
618+
// 非公开API 禁用
619+
Vue.util = {
620+
warn: warn,
621+
extend: extend,
622+
mergeOptions: mergeOptions,
623+
defineReactive: defineReactive$$1
624+
};
625+
626+
// 原型方法
627+
Vue.set = set;
628+
Vue.delete = del;
629+
Vue.nextTick = nextTick;
630+
631+
Vue.options = Object.create(null);
632+
config._assetTypes.forEach(function(type){
633+
Vue.options[type + 's'] = Object.create(null);
634+
});
635+
636+
// Vue.options._base.set = Vue.$set
637+
// ...
638+
Vue.options._base = Vue;
639+
640+
extend(Vue.options.components, buildInComponents);
641+
642+
initUse(Vue);
643+
initMixin$1(Vue);
644+
initExtend(Vue);
645+
initAssetRegisters(Vue);
646+
}
647+
648+
// 初始化全局API
649+
initGlobalAPI(vue$3);
650+
651+
Object.defineProperty(Vue$3.prototype, 'isServer', {
652+
get: isServerRendering
653+
});
654+
655+
vue$3.version = '2.2.6';
656+
```
657+
658+
659+
660+
---
340661

341662

342663

0 commit comments

Comments
 (0)