当前位置:网站首页>Analysis on the usage, response and global delivery of provide/inject
Analysis on the usage, response and global delivery of provide/inject
2022-06-13 04:42:00 【Dax1_】
Preface
Vue Value passed between parent and child of : props
Values are passed between components that are nested more deeply : provide/inject
More complex structure :vuex
provide/inject
Provide / Inject
provide
- An object or a function that returns an object . This object contains properties that can be injected into its descendants . You can use... In this object ES2015 Symbols As key, But only in native support Symbol and Reflect.ownKeys Working in an environment of .
inject
- An array of strings , Or an object
Use scenarios
For example, such a hierarchy
Root
└─ Components A
├─ Components B
└─ Components C
├─ Components D
└─ Components E
If you want to Components A
The data is passed directly to Components E
, We're going to prop Pass it on step by step : Components A
-> Components C
-> Components E
. And by provide/inject , Directly from Components A
Pass to Components E
Usage method
// Parent component / Ancestor component , Provide provide
// The first one is
export default {
name: "grandfather",
provide(){
return{
foo:'halo'
}
},
}
// The second kind
export default {
name: "grandfather",
provide:{
foo:'halo~~~~'
},
}
// Descendant components , Inject inject
export default {
inject:['foo'],
}
provide Method
Of the ancestor component provide Only the first method is recommended , That is, function writing . If the data you want to pass is just a string , There is no difference between the two methods , But the second way ( Object writing ) Cannot pass object .
provide/inject Not responsive
Official documents
provide and inject Binding is not responsive . This is intentional . However , If you pass in a listener object , Then the properties of its objects are still responsive .
We can pass an object , To achieve data responsive .
// Ancestor component
data() {
return {
obj:{
name:'dax1'},
}
}
provide(){
return{
username:this.obj // here provide An object
}
},
// Descendant components
export default {
inject: ['username']
}
here username
It's responsive data , because The properties of an object are responsive .
Achieve global delivery
provide/inject It can only be passed from ancestor to offspring , But it can be done by App.vue
binding provide, All subcomponents can be injected inject, So as to achieve global transmission .
Reference documents
边栏推荐
- Tita绩效宝:远程一对一面谈的问题
- Your one-on-one meetings are inefficient. You can do this!
- Crawler scrapy framework learning 2
- Online audio adjustment technology summary
- Ctfshow SQL injection (211-230)
- General communication protocol for industrial Internet
- 2022 ICLR | CONTRASTIVE LEARNING OF IMAGE- AND STRUCTURE BASED REPRESENTATIONS IN DRUG DISCOVERY
- The differences between the four startup modes of activity and the applicable scenarios and the setting methods of the two startup modes
- 【剑指Offer】面试题25.合并两个有序的链表
- Collection of wrong questions in soft test -- morning questions in the first half of 2011
猜你喜欢
Catalan number
Explanation of line segment tree
Common terms of electromagnetic compatibility
Express scaffold creation
Collection of wrong questions in soft test -- morning questions in the first half of 2010
小程序基础入门(黑马学习笔记)
Record a troubleshooting process - video call cannot be picked up
Ladder race
Tree array explanation
Applet - uniapp realizes the functions of two-dimensional code picture pop-up and picture saving
随机推荐
你的一对一会议效率低下,你可以这么做!
Basic syntax example for go
Idea Download
[chapter 67 of the flutter problem series] the solution to the problem that the get plug-in cannot jump to the route twice in the dialog pop-up window in flutter
PHP security development 15 user password modification module
Time format method on the official demo of uniapp
利用Javeswingjdbc基于mvc设计系统
记录一次排查问题的经过——视频通话无法接起
php开发14 友情链接模块的编写
How to implement a custom jdbc driver in only four steps?
It's the Caesar code. (*‘▽‘*)*
Redis master-slave replication, sentinel mode, cluster
PHP syntax
Collection of wrong questions in soft test -- morning questions in the first half of 2011
XOR prefix and +map maintenance
Online audio adjustment technology summary
Cesium:CesiumLab制作影像切片与切片加载
2022 ICML | Pocket2Mol: Efficient Molecular Sampling Based on 3D Protein Pockets
Third party comment plugin
Implementation of article list function on PHP 18 home page