/* @flow */ import { hasSymbol } from 'core/util/env' import { warn } from '../util/index' import { defineReactive } from '../observer/index' export function initProvide (vm: Component) { const provide = vm.$options.provide if (provide) {
// 計算proved結果 vm._provided = typeof provide === 'function' ? provide.call(vm) : provide } } export function initInjections (vm: Component) { const inject: any = vm.$options.inject if (inject) { // inject is :any because flow is not smart enough to figure out cached // isArray here
// 注入 可以任何類型 是因為fow不過智能, 計算出被緩存的數組
const isArray = Array.isArray(inject)
//如果是對象, 取出屬性值 const keys = isArray ? inject : hasSymbol ? Reflect.ownKeys(inject) : Object.keys(inject) for (let i = 0; i < keys.length; i++) { const key = keys[i] const provideKey = isArray ? key : inject[key] let source = vm
while (source) { if (source._provided && provideKey in source._provided) { /* istanbul ignore else */ if (process.env.NODE_ENV !== 'production') {
//添加這個vm的inject屬性監控 defineReactive(vm, key, source._provided[provideKey], () => { //避免直接監控一個屬性, 因為當重新渲染的時候, 這個監控會被改變
warn( `Avoid mutating an injected value directly since the changes will be ` + `overwritten whenever the provided component re-renders. ` + `injection being mutated: "${key}"`, vm ) }) } else { defineReactive(vm, key, source._provided[provideKey]) } break } source = source.$parent } } } }
這個方法應該是給vm的inject屬性添加監控.
