MCPcopy Index your code
hub / github.com/ygs-code/vue / initInjections

Function initInjections

vue.js:5314–5342  ·  view source on GitHub ↗
(vm)

Source from the content-addressed store, hash-verified

5312
5313 //初始化 inject
5314 function initInjections(vm) {
5315 //provide 和 inject 主要为高阶插件/组件库提供用例。并不推荐直接用于应用程序代码中。
5316 //这对选项需要一起使用,以允许一个祖先组件向其所有子孙后代注入一个依赖,不论组件层次有多深,并在起上下游关系成立的时间里始终生效。如果你熟悉 React,这与 React 的上下文特性很相似。
5317 //更多详情信息https://cn.vuejs.org/v2/api/#provide-inject
5318 var result = resolveInject(vm.$options.inject, vm);
5319 if (result) {
5320 toggleObserving(false);
5321 Object.keys(result).forEach(function (key) { //注入的值不能修改,相当于props属性一样
5322 /* istanbul ignore else */
5323 {
5324 // 通过defineProperty的set方法去通知notify()订阅者subscribers有新的值修改
5325 // * 添加观察者 get set方法
5326 defineReactive(
5327 vm,
5328 key,
5329 result[key],
5330 function () {
5331 warn(
5332 "Avoid mutating an injected value directly since the changes will be " +
5333 "overwritten whenever the provided component re-renders. " +
5334 "injection being mutated: \"" + key + "\"",
5335 vm
5336 );
5337 });
5338 }
5339 });
5340 toggleObserving(true);
5341 }
5342 }
5343
5344 // inject 选项应该是一个字符串数组或一个对象,该对象的 key 代表了本地绑定的名称,value 为其 key (字符串或 Symbol) 以在可用的注入中搜索。
5345 function resolveInject(inject, vm) {

Callers 1

initMixinFunction · 0.85

Calls 3

resolveInjectFunction · 0.85
toggleObservingFunction · 0.85
defineReactiveFunction · 0.85

Tested by

no test coverage detected