export function initGlobalAPI(Vue){
const configDef = {}
configDef.get = config
...
Object.defineProperty(Vue,'config',configDef)
Vue.util = {
warn,extend,mergeOptions,defineReactive
}
Vue.set = set
Vue.delete = del
Vue.nextTick = nextTick
Vue.observable = (obj) => {
observe(obj)
return obj
}
Vue.options = Object.create(null)
ASSET_TYPES.forEach(type => {
Vue.options[type + 's'] = Object.create(null)
})
Vue.options._base = Vue
extend(Vue.options.components,builtInComponents)
initUse(Vue)
initMixin(Vue)
initExtend(Vue)
initAssetRegisters(Vue)
}
builtInComponents
export default {
name:'keep-alive',
abstract:true,
props:{
include:[String,RegExp,Array],
exclude:[String,RegExp,Array],
max:[String,Number]
},
created(){
this.cache = Object.create(null)
this.keys = []
},
destroyed(){
for(const ket in this.cache){
pruneCacheEntry(this.cache,key,this.keys)
}
},
mounted(){
this.$watch('include',val => {
pruneCache(this,name => matches(val,name))
})
this.$watch('exclude',val => {
pruneCache(this,name => !matches(val,name))
})
},
render(){
const slot = this.$slots.default
const vnode = getFirstComponentChild(slot)
const componentOptions = vnode && vnode.componentOptions
if(componentOptions){
const name = getComponentName(componentOptions)
const { include, exclude } = this
if((include && (!name || !matches(include,name))) || (exclude && name && matches(exclude,name))){
return vnode
}
const { cache, keys } = this
const key = vnode.key = null ? componentOptions.Ctor.cid + (componentOptions.tag ? `::${componentOptions.tag}` : '') : vnode.key
if(cache[key]){
vnode.componentInstance = cache[key].componentInstance
remove(keys,key)
keys.push(key)
}else{
cache[key] = vnode
keys.push(key)
if(this.max && keys.length > parseInt(this.max)){
pruneCacheEntry(cahce,key[0],keys,this._vnode)
}
}
vnode.data.keepAlive = true
}
return vnode || (slot && slot[0])
}
}
initUse
export function initUse(Vue){
Vue.use = function(plugin){
const installedPlugins = (this._installedPlugins || this._installedPlugins = [])
if(installedPlugins.indexOf(plugin) > -1){
return this
}
const args = toArray(arguments,1)
args.unshift(this)
if(typeof plugin.install === 'function'){
plugin.install.call(plugin,args)
}else if(typeof plugin === 'function' ){
plugin.call(null,args)
}
installedPlugins.push(plugin)
return this
}
}
initMixin
export function initMixin(Vue){
Vue.mixin = function(mixin){
this.options = mergeOptions(this.options,mixin)
return this
}
}
initExtend
export function initExtend(Vue){
Vue.cid = 0
let cid = 1
Vue.extend = function(extendOptions){
extendOptions = extendOptions || {}
const Super = this
const SuperId = Super.cid
const cacheCtors = extendOptions._Ctor|| (extendOptions._Ctor = {})
if(cacheCtors[SuperId]){
return cacheCtors[SuperId]
}
const name = extendOptions.name || super.options.name
if(process.env.NODE_ENV !== 'production' && name){
validComponentName(name)
}
const Sub = function VueComponent(options){
this._init(options)
}
Sub.prototype = Object.create(Super.prototype)
Sub.prototype.constructor = Sub
Sub.cid = cid ++
Sub.options = mergeOptions(Super.options,extendOptions)
Sub['super'] = Super
if(Sub.options.props){
initProps(Sub)
}
if(Sub.options.computed){
initComputed(Sub)
}
Sub.extend = Super.extend
Sub.mixin = Super.mixin
Sub.use = Super.use
ASSET_TYPES.forEach((type) => {
Sub[type] = Super[type]
})
if(name){
Sub.options.component[name] = Sub
}
Sub.superOptions = Super.options
Sub.extendOptions = extendOptions
Sub.sealedOptions = extend({},Sub.options)
cacheCtors[SuperId] = Sub
return Sub
}
}
initAssetRegisters
export function initAssetRegisters(Vue){
ASSET_TYPES.forEach(type => {
Vue[type] = function(id,definition){
if(!definition){
return this.options[type + 's'][id]
}else{
if(process.env.NODE_ENV !== 'production' && type === 'component'){
validComponentName(id)
}
if(type === 'component' && isPlainObject(definition)){
definition.name = definition.name || id
definition = this.options._base.extend(definition)
}
if(type === 'directive' && typeof difinition === 'function'){
definition = { bind: difinition, update: difinition }
}
this.options[type + 's'][id] = definition
return definition
}
}
})
}