provide/inject:就是在父组件中通过provider来提供变量,然后在子组件中通过inject来 深层次 注入变量。(无论目标组件里父组件有多少间隔)
需要注意的是这里不论子组件有多深,只要调用了inject那么就可以注入provide中的数据。而不是局限于只能从当前父组件的prop属性来获取数据。
一、app.vue
<router-view v-if="isRouterAlive" ></router-view>
export default {
name: 'app',
provide () { //父组件中通过provide来提供变量,在子组件中通过inject来注入变量。
return {
reload: this.reload
}
},
data() {
return {
isRouterAlive : true
}
},
methods:{
reload(){
this.isRouterAlive =false;
this.$nextTick(()=>{
this.isRouterAlive =true;
});
},
},
}
二、子页面child.vue
export default {
inject:['reload'],
}
现在可直接调用this.reload();了
数据更新时
isRouterAlive 从 true => false => this.$nextTick true
实现页面重载