vue刷新当前页面 reload【provide/inject】
vue刷新当前页面有挺多种方法,比如 window.location.reload() 或者 this.$router.go(0)
但是这两种方法是会出现一瞬间的白屏,体验不好,所以这里给大家推荐第三种比较好用的刷新页面的方法
完整例子
主要是利用provide/inject 的应用
home.vue 定义
<template><div><!-- 导航 --><Nav/><router-view v-if="isRouterAlive"/> // 2、通过isRouterAlive刷新路由</div></template><script>import Nav from './module/Nav.vue';export default {components:{Nav, // 侧边栏},data(){return {isRouterAlive: true // 1、这个属性名可以自己定义,默认值为true}},// 4、父组件提供reload方法provide () {return {reload: this.reload}},methods:{// 3、刷新的方法reload () {this.isRouterAlive = falsethis.$nextTick(function() {this.isRouterAlive = true})},}}</script>
Nav.vue 调用
<template><div><p v-for="item in menuList" @click="getRoute(item)">{{item.meta.name}}</p></div></template><script>export default{// 1、子组件获取home.vue定义的reload方法inject: ['reload'],data(){return {menuList: [{path: '/',name: 'Home',meta:{name: '首页'}},{path: '/list'name: 'List',meta:{name: '列表'}},{path: '/info'name: 'Info',meta:{name: '介绍'}}]}}methods: {getRoute(item){if(item.name == 'List')this.reload(); // 2、调用home.vue定义的方法}else{this.$router.push(item.name);}}}}</script>
