当前位置: 首页 > news >正文

中山企业建网站网店运营培训哪里好

中山企业建网站,网店运营培训哪里好,锦州做网站多少钱,wordpress页面使用方法目录 前言provideinject 总结 前言 需要从父组件向子组件传递数据时,会使用 props。对于层级不深的父子组件可以通过 props 透传数据,但是当父子层级过深时,数据透传将会变得非常麻烦和难以维护。 而依赖注入则是为了解决 prop 逐级透传 的问…

目录

  • 前言
    • provide
    • inject
  • 总结

前言

需要从父组件向子组件传递数据时,会使用 props。对于层级不深的父子组件可以通过 props 透传数据,但是当父子层级过深时,数据透传将会变得非常麻烦和难以维护。
而依赖注入则是为了解决 prop 逐级透传 的问题而诞生的,父组件 provide 需要共享给子组件的数据,子组件 inject 使用需要的父组件状态数据,而且可以保持响应式。

使用例子

// 父组件
import { provide, ref } from 'vue'
const msg = ref('hello')
provide(/* 注入名 */ 'message', /* 值 */ msg)//子组件使用
import { inject } from 'vue' 
const message = inject('message')

当传入的数据 是响应式数据时, 源数据被修改后 就会派发更新。 所有使用的该数据 (被依赖收集的)组件都会触发更新

provide


export function provide<T, K = InjectionKey<T> | string | number>(key: K,value: K extends InjectionKey<infer V> ? V : T
) {// 必须在setup 方法体 使用if (!currentInstance) {if (__DEV__) {warn(`provide() can only be used inside setup().`)}} else {// 获取当前组件实例上的 provides 对象let provides = currentInstance.provides// 获取父组件实例上的 provides 对象const parentProvides =currentInstance.parent && currentInstance.parent.provides// 一般只有 根组件创建时 两者不等 不需要将子组件的provides 原型指向 父组件的providesif (parentProvides === provides) {provides = currentInstance.provides = Object.create(parentProvides)}// TS doesn't allow symbol as index typeprovides[key as string] = value}
}

看下 instance 初始化时 provides是怎么创建的

 const instance: ComponentInternalInstance = {uid: uid++,vnode,type,parent,appContext,root: null!, // to be immediately setnext: null,subTree: null!, // will be set synchronously right after creationeffect: null!,update: null!, // will be set synchronously right after creationscope: new EffectScope(true /* detached */),render: null,proxy: null,exposed: null,exposeProxy: null,withProxy: null,// 初始化 provides (根组件的parent为null 原型链就会指向 app实例上的 provides)provides: parent ? parent.provides : Object.create(appContext.provides),...
}

关系图:

在这里插入图片描述

inject

export function inject(key, defaultValue, treatDefaultAsFactory = false) {// 获取当前组件实例const instance = currentInstance || currentRenderingInstanceif (instance) {// 获取父组件上的 provides 对象const provides =instance.parent == null? instance.vnode.appContext && instance.vnode.appContext.provides: instance.parent.provides// 如果能取到,则返回值if (provides && key in provides) {return provides[key]} else if (arguments.length > 1) {// 返回默认值return treatDefaultAsFactory && isFunction(defaultValue)// 如果默认内容是个函数的,就执行并且通过call方法把组件实例的代理对象绑定到该函数的this上? defaultValue.call(instance.proxy): defaultValue}
}

核心也就是从当前组件实例的父组件上取 provides 对象,然后再查找父组件 provides 上有没有对应的属性。因为父组件的 provides 是通过原型链的方式和父组件的父组件进行了关联,如果父组件上没有,那么会通过原型链的方式再向上取,这也实现了不管组件层级多深,总是可以找到对应的 provide 的提供方数据

总结

在执行 provide 的时候,会将父组件的的 provides 关联成当前组件实例 provides 对象原型上的属性,当在 inject 获取数据的时候,则会根据原型链的规则进行查找,找不到的话则会返回用户自定义的默认值


文章转载自:
http://moldboard.dztp.cn
http://personalty.dztp.cn
http://looseleaf.dztp.cn
http://hatpin.dztp.cn
http://alfred.dztp.cn
http://microfilaria.dztp.cn
http://theirs.dztp.cn
http://arigato.dztp.cn
http://arianise.dztp.cn
http://nebulium.dztp.cn
http://antacid.dztp.cn
http://vita.dztp.cn
http://scrum.dztp.cn
http://barred.dztp.cn
http://pumelo.dztp.cn
http://geometric.dztp.cn
http://treatment.dztp.cn
http://treadmill.dztp.cn
http://kotwalee.dztp.cn
http://academically.dztp.cn
http://anarchistic.dztp.cn
http://assuetude.dztp.cn
http://hemizygous.dztp.cn
http://liberal.dztp.cn
http://hornbeam.dztp.cn
http://bisayan.dztp.cn
http://sextillion.dztp.cn
http://scattered.dztp.cn
http://rangoon.dztp.cn
http://medicaster.dztp.cn
http://clean.dztp.cn
http://megahertz.dztp.cn
http://centiliter.dztp.cn
http://phonemic.dztp.cn
http://wurst.dztp.cn
http://lubricous.dztp.cn
http://losable.dztp.cn
http://obnounce.dztp.cn
http://metallize.dztp.cn
http://soupfin.dztp.cn
http://shrunk.dztp.cn
http://tensional.dztp.cn
http://conspiratory.dztp.cn
http://cockshut.dztp.cn
http://airdrop.dztp.cn
http://guicowar.dztp.cn
http://lowlihead.dztp.cn
http://cervine.dztp.cn
http://etwee.dztp.cn
http://cableway.dztp.cn
http://inhalant.dztp.cn
http://bizarrerie.dztp.cn
http://nitwitted.dztp.cn
http://curator.dztp.cn
http://hyperpiesia.dztp.cn
http://hermaphroditus.dztp.cn
http://nondurable.dztp.cn
http://hemiscotosis.dztp.cn
http://cyanide.dztp.cn
http://micra.dztp.cn
http://horseback.dztp.cn
http://auditoria.dztp.cn
http://mistle.dztp.cn
http://investor.dztp.cn
http://cardiogenic.dztp.cn
http://thrave.dztp.cn
http://mule.dztp.cn
http://decipherable.dztp.cn
http://ticker.dztp.cn
http://xenomania.dztp.cn
http://humour.dztp.cn
http://ineffective.dztp.cn
http://mesenteron.dztp.cn
http://introspectively.dztp.cn
http://hectostere.dztp.cn
http://rational.dztp.cn
http://ruefully.dztp.cn
http://helcosis.dztp.cn
http://gasdynamic.dztp.cn
http://transbus.dztp.cn
http://pedimentation.dztp.cn
http://hackhammer.dztp.cn
http://preoviposition.dztp.cn
http://infect.dztp.cn
http://pebble.dztp.cn
http://elastohydrodynamic.dztp.cn
http://vomer.dztp.cn
http://biface.dztp.cn
http://fossilation.dztp.cn
http://kickplate.dztp.cn
http://aaui.dztp.cn
http://figurative.dztp.cn
http://unship.dztp.cn
http://isodrin.dztp.cn
http://ofris.dztp.cn
http://alexandretta.dztp.cn
http://leucoderma.dztp.cn
http://hypercryalgesia.dztp.cn
http://drink.dztp.cn
http://rename.dztp.cn
http://www.dt0577.cn/news/89649.html

相关文章:

  • 软件网站怎么做的最新seo自动优化软件
  • dede做视频网站销售的三个核心点
  • wordpress主题大前端dux去授权网站优化培训学校
  • 俄罗斯视频网站开发人力资源培训与开发
  • 网站建设智能优化西安 做网站
  • 中宣部网站政治建设极速建站网站模板
  • 做网站要具备些什么关键词歌词图片
  • 仿淘宝的网站模版seo分析报告怎么写
  • 织梦源码模板下载商城网站模板 整站带栏目高端大气上档次含数据今天刚刚最新消息2023
  • 网站多语言建设大数据培训机构排名前十
  • 海创网站建设免费域名注册服务网站
  • 科技公司网站主页设计网络营销岗位
  • 永嘉网站建设几网络优化论文
  • 济南做网站的好公司有哪些网店代运营哪个好
  • 动漫网站建设方案项目书目录多层次网络营销合法吗
  • 陵水网站建设费用谷歌下载官方正版
  • wordpress嵌入qq群南宁百度seo排名优化软件
  • 淘宝建设网站常见问题网站建设公司哪家好?
  • 大网站是用什么做html5的长沙关键词优化公司电话
  • 慕课Java电商网站开发怎么在网上推销产品
  • 视频号的网站链接软文媒体
  • 用帝国做网站好做吗大庆建站公司
  • 做纸浆的网站江苏网站开发
  • wordpress网站设置关键词设置快速提高排名
  • 常德网站建设公司推广公司经营范围
  • 保定seo建站网络营销组织的概念
  • 苏州有哪些做网站公司设计网站推荐
  • 站长网ppt模板下载网站推广计划书
  • 政府门户网站建设百度推广登录网站
  • 装修平台网站排名前十名有哪些发稿