vue中的provide/inject讲解
作者:互联网
最近在看element-ui的源码,发现了一个这样的属性:inject.遂查看官网provider/inject
provider/inject:简单的来说就是在父组件中通过provider来提供变量,然后在子组件中通过inject来注入变量。
需要注意的是这里不论子组件有多深,只要调用了inject那么就可以注入provider中的数据。而不是局限于只能从当前父组件的prop属性来获取数据。
下面我们来验证下猜想:
- first:定义一个parent component
<template>
<div>
<childOne></childOne>
</div>
</template>
<script>
import childOne from '../components/test/ChildOne'
export default {
name: "Parent",
provide: {
for: "demo"
},
components:{
childOne
}
}
在这里我们在父组件中provide for这个变量。
- second 定义一个子组件
<template>
<div>
{{demo}}
<childtwo></childtwo>
</div>
</template>
<script>
import childtwo from './ChildTwo'
export default {
name: "childOne",
inject: ['for'],
data() {
return {
demo: this.for
}
},
components: {
childtwo
}
}
</script>
- third 定义另一个子组件
<template>
<div>
{{demo}}
</div>
</template>
<script>
export default {
name: "",
inject: ['for'],
data() {
return {
demo: this.for
}
}
}
</script>
在2个子组件中我们使用jnject注入了provide提供的变量for,并将它提供给了data属性。
这里官网注明例子只工作在 Vue 2.2.1 或更高版本。低于这个版本时,注入的值会在 props 和 data 初始化之后得到。
运行之后看下结果
从上面这个例子可以看出,只要在父组件中调用了,那么在这个父组件生效的生命周期内,所有的子组件都可以调用inject来注入父组件中的值。
http://market.szonline.net/amaz/18716.html
http://market.szonline.net/amaz/18715.html
http://market.szonline.net/amaz/18713.html
http://market.szonline.net/amaz/18712.html
http://market.szonline.net/amaz/18711.html
http://market.szonline.net/amaz/18709.html
http://market.szonline.net/amaz/18708.html
http://market.szonline.net/amaz/18699.html
http://market.szonline.net/amaz/18698.html
http://market.szonline.net/amaz/18697.html
http://market.szonline.net/amaz/18696.html
http://market.szonline.net/amaz/18695.html
http://market.szonline.net/amaz/18690.html
http://market.szonline.net/amaz/18689.html
http://market.szonline.net/amaz/18688.html
http://market.szonline.net/amaz/18687.html
http://market.szonline.net/amaz/18686.html
http://market.szonline.net/amaz/18685.html
http://market.szonline.net/amaz/18680.html
http://market.szonline.net/amaz/18677.html
http://market.szonline.net/amaz/18725.html
http://market.szonline.net/amaz/18724.html
http://market.szonline.net/amaz/18723.html
http://market.szonline.net/amaz/18653.html
http://market.szonline.net/amaz/18652.html
http://market.szonline.net/amaz/18650.html
http://market.szonline.net/amaz/18648.html
http://market.szonline.net/amaz/18647.html
http://market.szonline.net/amaz/18645.html
http://market.szonline.net/amaz/18643.html
http://market.szonline.net/amaz/18641.html
http://market.szonline.net/amaz/18640.html
http://market.szonline.net/amaz/18639.html
http://market.szonline.net/amaz/18638.html
http://market.szonline.net/amaz/18637.html
http://market.szonline.net/amaz/18636.html
http://market.szonline.net/amaz/18635.html
http://market.szonline.net/amaz/18634.html
http://market.szonline.net/amaz/18633.html
http://market.szonline.net/amaz/18632.html
http://market.szonline.net/amaz/18631.html
http://www.72177.com/htm/201910/23/4501366.htm
http://www.72177.com/htm/201910/23/4501365.htm
http://www.72177.com/htm/201910/23/4501364.htm
http://www.72177.com/htm/201910/23/4501363.htm
http://www.72177.com/htm/201910/23/4501362.htm
http://www.72177.com/htm/201910/23/4501361.htm
http://www.72177.com/htm/201910/23/4501360.htm
http://www.72177.com/htm/201910/23/4501359.htm
http://www.72177.com/htm/201910/23/4501358.htm
http://www.72177.com/htm/201910/23/4501357.htm
http://www.72177.com/htm/201910/23/4501356.htm
http://www.72177.com/htm/201910/23/4501355.htm
http://www.72177.com/htm/201910/23/4501354.htm
http://www.72177.com/htm/201910/23/4501353.htm
http://www.72177.com/htm/201910/23/4501352.htm
http://www.72177.com/htm/201910/23/4501351.htm
http://www.72177.com/htm/201910/23/4501350.htm
http://www.72177.com/htm/201910/23/4501349.htm
http://www.72177.com/htm/201910/23/4501348.htm
http://www.72177.com/htm/201910/23/4501347.htm
http://www.72177.com/htm/201910/23/4501345.htm
http://www.72177.com/htm/201910/23/4501344.htm
http://www.72177.com/htm/201910/23/4501342.htm
http://www.72177.com/htm/201910/23/4501340.htm
http://www.72177.com/htm/201910/23/4501338.htm
http://www.72177.com/htm/201910/23/4501336.htm
http://market.szonline.net/amaz/18676.html
http://market.szonline.net/amaz/18675.html
http://market.szonline.net/amaz/18674.html
http://market.szonline.net/amaz/18673.html
http://market.szonline.net/amaz/18672.html
http://market.szonline.net/amaz/18671.html
http://market.szonline.net/amaz/18670.html
http://market.szonline.net/amaz/18669.html
http://market.szonline.net/amaz/18668.html
http://market.szonline.net/amaz/18667.html
http://market.szonline.net/amaz/18666.html
http://market.szonline.net/amaz/18665.html
标签:vue,provide,szonline,inject,net,htmlhttp,htm,market,amaz 来源: https://www.cnblogs.com/zjw2004112/p/11727305.html