当前位置:网站首页>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
边栏推荐
- Collection of wrong questions in soft test -- morning questions in the first half of 2010
- 2022 ICLR | CONTRASTIVE LEARNING OF IMAGE- AND STRUCTURE BASED REPRESENTATIONS IN DRUG DISCOVERY
- NodeJS 解析 GET 请求 url 字符串
- PowerShell plus domain add computer module
- D 小红的构造题
- 2022 ICLR | CONTRASTIVE LEARNING OF IMAGE- AND STRUCTURE BASED REPRESENTATIONS IN DRUG DISCOVERY
- The problem that ionic3 cannot automatically install the APK package
- Returns the width and height of an element
- Trust programming - linked lists: use struct to implement linked lists, use heap to merge K ascending linked lists, and customize display
- rust编程-链表:使用struct实现链表,使用堆合并k个升序链表,自定义Display
猜你喜欢
Sword finger offer 56 - I. number of occurrences in the array
CTFSHOW SQL注入篇(211-230)
PowerShell:因为在此系统上禁止运行脚本,解决方法
PowerShell: because running scripts is prohibited on this system, the solution
【JS解决】leedcode 200. 岛屿数量
Converting MySQL data to PostgreSQL with Navicat
Express framework knowledge - Art template template, cookie, session
正态分布(高斯分布)
[JS solution] leedcode 200 Number of islands
Internet people a few years ago vs Internet people today
随机推荐
CreateAnonymousThreadX给匿名线程传递参数
Ultra quicksort reverse sequence pair
Applet - uniapp realizes the functions of two-dimensional code picture pop-up and picture saving
如何优雅的处理async/await错误信息
Time format method on the official demo of uniapp
Returns the width and height of an element
Ladder race
剑指 Offer 56 - I. 数组中数字出现的次数
D 小红的构造题
Construction problem of D Xiaohong
ES6 learning
【JS解决】leedcode 200. 岛屿数量
Li Kou brush question 338 Bit count
php开发14 友情链接模块的编写
CTFSHOW SQL注入篇(211-230)
CTFSHOW SQL注入篇(231-253)
Explanation of line segment tree
Notes on software test for programmers -- basic knowledge of software development, operation and maintenance
[sword finger offer] interview question 25 Merge two ordered linked lists
小C的记事本