vue.js 源代碼學習筆記 ----- instance inject

 2023-12-25 阅读 292 评论 0

摘要:/* @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.provideif
/* @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.provideif (provide) {
  // 計算proved結果vm._provided
= typeof provide === 'function'? provide.call(vm): provide} }export function initInjections (vm: Component) {const inject: any = vm.$options.injectif (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屬性添加監控.

轉載于:https://www.cnblogs.com/dhsz/p/7116409.html

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/1/195019.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息