运用 provide-inject方法来实现组件的局部刷新
- 在APP.vue当中添加:
//在template当中添加一个v-if判断
<router-view v-if="isRouterAlive"/>
//在data当中添加变量
data(){
return{
isRouterAlive:true
}
}
//在methods当中添加方法
methods:{
//通过改变isRouterAlive来改变路由的key
reload(){
this.isRouterAlive = false
this.$nextTick(function () {
this.isRouterAlive = true
})
}
}
//在最开始data上面通过provide把reload挂载上去
provide(){
return {
reload:this.reload
}
},
- 在组件的页面当中添加
export default {
//在子组件当中通过 inject获取父组件传过来的reload
inject:["reload"],
methods:{
//在方法当中调用reload()函数
this.reload()
}
}