* Normalize all injections into Object-based format * 将所有注入规范化为基于对象的格式 * * * 将数组转化成对象 比如 [1,2,3]转化成 * normalized[1]={from: 1} * normalized[2]={from: 2} * normalized[3]={from: 3} * * * *
(options, vm)
| 2054 | * * |
| 2055 | */ |
| 2056 | function normalizeInject(options, vm) { |
| 2057 | // provide 和 inject 主要为高阶插件/组件库提供用例。并不推荐直接用于应用程序代码中。 |
| 2058 | // 这对选项需要一起使用,以允许一个祖先组件向其所有子孙后代注入一个依赖,不论组件层次有多深,并在起上下游关系成立的时间里始终生效。如果你熟悉 React,这与 React 的上下文特性很相似。 |
| 2059 | |
| 2060 | var inject = options.inject; |
| 2061 | if (!inject) { |
| 2062 | return |
| 2063 | } |
| 2064 | var normalized = options.inject = {}; |
| 2065 | if (Array.isArray(inject)) { //如果是数组 |
| 2066 | for (var i = 0; i < inject.length; i++) { |
| 2067 | // * 将数组转化成对象 比如 [1,2,3]转化成 |
| 2068 | // * normalized[1]={from: 1} |
| 2069 | // * normalized[2]={from: 2} |
| 2070 | // * normalized[3]={from: 3} |
| 2071 | normalized[inject[i]] = { from: inject[i] }; |
| 2072 | } |
| 2073 | } else if (isPlainObject(inject)) { //如果是对象 |
| 2074 | for (var key in inject) { |
| 2075 | var val = inject[key]; |
| 2076 | normalized[key] = isPlainObject(val) ? extend({ from: key }, val) : { from: val }; |
| 2077 | } |
| 2078 | } else { |
| 2079 | warn( |
| 2080 | "Invalid value for option \"inject\": expected an Array or an Object, " + |
| 2081 | "but got " + (toRawType(inject)) + ".", |
| 2082 | vm |
| 2083 | ); |
| 2084 | } |
| 2085 | } |
| 2086 | |
| 2087 | /** |
| 2088 | * Normalize raw function directives into object format. |
no test coverage detected