Vue重载组件(常用)
activated / inject
<baidu-map
v-if="mapReload" />
activated() {
this.mapReload = true
this.doSomething()
},
deactivated() {
this.mapReload = false
this.doSomething()
},
provide/inject 当我们想在任何时候刷新这个页面时,例如当ajax请求完成时需要重载页面以获得更新,如果用location.reload() 体验感会很差,相当于用户刷新了这个页面。而采用provide/inject 这种方式原理是将<router-view/> 先隐藏再显示,达到重新渲染路由区域的效果,体验感极好
<router-view v-if="isRouterAlive" />
provide() {
return {
reload: this.reload
}
},
data() {
return {
isRouterAlive: true
}
},
methods: {
reload() {
this.isRouterAlive = false
this.$nextTick(() => {
this.isRouterAlive = true
})
}
}
inject: ['reload'],
data() {
return {
}
}
method() {
this.$get('path').then(res => {
this.reload()
})
}
|