当前位置:网站首页>组件传值 provide/inject
组件传值 provide/inject
2022-07-31 03:19:00 【大象与小蚂蚁的生活】
1.简介
1.Vue2.2.0 新增 API,这对选项需要一起使用,以允许一个祖先组件向其所有子孙后代注入一个依赖,不论组件层次有多深,并在起上下游关系成立的时间里始终生效。
2.一言而蔽之:祖先组件中通过 provider 来提供变量,然后在子孙组件中通过 inject 来注入变量。
provide / inject API 主要解决了跨级组件间的通信问题,不过它的使用场景,主要是子组件获取上级组件的状态,跨级组件间建立了一种主动提供与依赖注入的关系。
2.举个例子
假设有两个组件: A.vue 和 B.vue,B 是 A 的子组件
// A.vue
export default {
provide: {
name: '浪里行舟'
}
}
// B.vue
export default {
inject: ['name'],
mounted () {
console.log(this.name); // 浪里行舟
}
}

3.provide 与 inject 怎么实现数据响应式
一般来说,有两种办法:
- provide 祖先组件的实例,然后在子孙组件中注入依赖,这样就可以在子孙组件中直接修改祖先组件的实例的属性,不过这种方法有个缺点就是这个实例上挂载很多没有必要的东西比如 props,methods 等
- 使用 2.6 最新 API Vue.observable 优化响应式 provide (推荐)

// A 组件
<div>
<h1>A 组件</h1>
<button @click="() => changeColor()">改变color</button>
<ChildrenB />
<ChildrenC />
</div>
......
data() {
return {
color: "blue"
};
},
// provide() {
// return {
// theme: {
// color: this.color //这种方式绑定的数据并不是可响应的
// } // 即A组件的color变化后,组件D、E、F不会跟着变
// };
// },
provide() {
return {
theme: this//方法一:提供祖先组件的实例
};
},
methods: {
changeColor(color) {
if (color) {
this.color = color;
} else {
this.color = this.color === "blue" ? "red" : "blue";
}
}
}
// 方法二:使用2.6最新API Vue.observable 优化响应式 provide
// provide() {
// this.theme = Vue.observable({
// color: "blue"
// });
// return {
// theme: this.theme
// };
// },
// methods: {
// changeColor(color) {
// if (color) {
// this.theme.color = color;
// } else {
// this.theme.color = this.theme.color === "blue" ? "red" : "blue";
// }
// }
// }
// F 组件
<template functional>
<div class="border2">
<h3 :style="{ color: this.theme.color }">F 组件</h3>
</div>
</template>
<script>
export default {
inject: ["theme"]
// inject: {
// theme: {
// from: theme,
// //函数式组件取值不一样
// default: () => ({})
// }
// }
};
</script>
虽说 provide 和 inject 主要为高阶插件/组件库提供用例,但如果你能在业务中熟练运用,可以达到事半功倍的效果!
有关 inject 参数为对象的用法可见 provide / inject
4.项目中使用
provide() {
return {
reload: this.reload
};
},
provide() {
return {
_getMapRoot: () => {
return this;
},
// 向子组件提供地图实例
_getMapRootMap: this.getMap,
};
},
边栏推荐
- Thesis framework of the opening report
- What is a distributed lock?Three ways of implementing distributed lock
- Day32 LeetCode
- 【C语言】求两个整数m和n的最大公因数和最小公倍数之和一般方法,经典解法
- 【CocosCreator 3.5】CocosCreator get network status
- IDEA 注释报红解决
- Map.Entry理解和应用
- SQL injection Less47 (error injection) and Less49 (time blind injection)
- postgresql 15源码浅析(5)—— pg_control
- C primer plus学习笔记 —— 8、结构体
猜你喜欢

浅识Flutter 基本组件之showDatePicker方法

postgresql 15源码浅析(5)—— pg_control

Detailed explanation of TCP (1)
![[Compilation principle] Lexical analysis program design principle and implementation](/img/eb/035234a402edf18beb7e2f82ebec17.png)
[Compilation principle] Lexical analysis program design principle and implementation

10 Permission introduction

web容器及IIS --- 中间件渗透方法1

5. SAP ABAP OData 服务如何支持 $filter (过滤)操作

MP使用时的几个常见报错

Problems that need to be solved in distributed system architecture

Recursive query single table - single table tree structure - (self-use)
随机推荐
【编译原理】词法分析程序设计原理与实现
浅识Flutter 基本组件之CheckboxListTile组件
大小端模式
els 方块向左移动条件判断
postgresql 15源码浅析(5)—— pg_control
[Godot][GDScript] 2D cave map randomly generated
安全20220709
VS QT——ui不显示新添加成员(控件)||代码无提示
Day32 LeetCode
PMP WeChat group daily exercises
Addition and Subtraction of Scores in LeetCode Medium Questions
What is distributed and clustered?What is the difference?
Ambiguous method call.both
【C语言】求两个整数m和n的最大公因数和最小公倍数之和一般方法,经典解法
els 方块向右移动边界判断、向下加速
Distributed locks and three implementation methods
10 Permission introduction
Map.Entry理解和应用
日志级别 和 打印log注意
Detailed explanation of TCP (1)