Vue 2.0 中依赖注入 provide/inject组合实战(2)

执行_init()时会执行mergeOptions()进行数据的合并,对于provide的合并策略等于mergeDataOrFn()函数(和data的合并策略是一样的,定义在1321行),返回一个匿名函数(第1154行),如下:

function mergeDataOrFn ( //第1154行 parentVal, childVal, vm ) { if (!vm) { //这是组件的分支 // in a Vue.extend merge, both should be functions if (!childVal) { return parentVal } if (!parentVal) { return childVal } // when parentVal & childVal are both present, // we need to return a function that returns the // merged result of both functions... no need to // check if parentVal is a function here because // it has to be a function to pass previous merges. return function mergedDataFn () { return mergeData( typeof childVal === 'function' ? childVal.call(this, this) : childVal, typeof parentVal === 'function' ? parentVal.call(this, this) : parentVal ) } } else { //这是非组件的实例,返回一个函数 return function mergedInstanceDataFn () { // instance merge var instanceData = typeof childVal === 'function' ? childVal.call(vm, vm) : childVal; var defaultData = typeof parentVal === 'function' ? parentVal.call(vm, vm) : parentVal; if (instanceData) { return mergeData(instanceData, defaultData) } else { return defaultData } } } }

然后返回到_init()之后会调用initProvide()初始化provide:

function initProvide (vm) { //第3619行 var provide = vm.$options.provide; //尝试获取provide if (provide) { //如果provide存在,当它是函数时执行该返回,否则直接将provide保存到Vue实例的_provided属性上 vm._provided = typeof provide === 'function' ? provide.call(vm) : provide; } }

返回后_provided等于{message:"Hello Vue!"},如下

e]

Vue 2.0 中依赖注入 provide/inject组合实战

组件实例化时

_init()时会执行initInjections(),经过了前面两步的处理,这里比较简单了,直接从父Vue或父Vue的父Vue获取对应的值即可,如下:

function initInjections (vm) { //第2681行 初始化inject var result = resolveInject(vm.$options.inject, vm); //遍历祖先节点,获取对应的inject,例如:比如:{foo: "bar"} if (result) { //如果获取了对应的值,则将它变成响应式 toggleObserving(false); Object.keys(result).forEach(function (key) { /* istanbul ignore else */ { defineReactive(vm, key, result[key], function () {   //将key编程响应式,这样就可以访问该元素了 warn( "Avoid mutating an injected value directly since the changes will be " + "overwritten whenever the provided component re-renders. " + "injection being mutated: \"" + key + "\"", vm ); }); } }); toggleObserving(true); } } function resolveInject (inject, vm) { //第3649行 确定Inject inject:例如:{foo: {from: "foo"}} vm:当前组件的实例 if (inject) { //如果inject非空 // inject is :any because flow is not smart enough to figure out cached var result = Object.create(null); //存储最后的结果 var keys = hasSymbol ? Reflect.ownKeys(inject).filter(function (key) { //如果有符号类型,调用Reflect.ownKeys()返回所有的key,再调用filter /* istanbul ignore next */ return Object.getOwnPropertyDescriptor(inject, key).enumerable }) : Object.keys(inject); //获取所有的key,此时keys就是个字符串数组,比如:["foo"] for (var i = 0; i < keys.length; i++) { //这里遍历每个key var key = keys[i]; var provideKey = inject[key].from; var source = vm; while (source) { if (source._provided && hasOwn(source._provided, provideKey)) { //如果source存在_provided 且 含有provideKey这个属性 result[key] = source._provided[provideKey]; //则将值保存到result[key]中 break //并跳出while循环 } source = source.$parent; //否则将source赋值给父Vue实例,直到找到对应的providekey为止 } if (!source) { //如果最后source不存在,即没有从当前实例或祖先实例的_provide找到privideKey这个key if ('default' in inject[key]) { var provideDefault = inject[key].default; //如果有定义defult,则使用默认值 result[key] = typeof provideDefault === 'function' ? provideDefault.call(vm) : provideDefault; } else { warn(("Injection \"" + key + "\" not found"), vm); } } } return result //返回结果,比如:{foo: "bar"} } }

注:provide 和 inject 绑定并不是可响应的。这是刻意为之的。然而,如果你传入了一个可监听的对象,那么其对象的属性还是可响应的。

总结

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/d2521ea65bcc3c110be5456aab3d3cee.html