vue router 刷新当前页面并传值

版权声明:Yangliwei 版权所有 https://blog.csdn.net/qq_32674347/article/details/87952120

1,vue-router在路由是当前页面时,不能刷新,router.push不能用

2,可以用强制刷新

this.$router.go(0)
location.reload()

3,使用provide / inject

在app.vue中写入如下代码:

<template>
  <div id="app">
      <router-view v-if="isRouterAlive"></router-view>
  </div>
</template>

<script>
export default {
  name: 'app',
  provide () {
    return {
      reload: this.reload
    }
  },
  data () {
    return {
      isRouterAlive:true,
    }
  },
  methods: {
    reload () {
      this.isRouterAlive = false
      this.$nextTick(function () {
        this.isRouterAlive = true
      })
    }
  }
}
</script>
<style>
</style>

在组件中,修改需要刷新的组件

export default {
  inject: ['reload'],
}

在需要刷新的方法中写入

this.reload();

3,这个貌似只对这种格式的路由起作用

{
    name:"search",
    path:"/search/:searchText",
    component:search
  }

猜你喜欢

转载自blog.csdn.net/qq_32674347/article/details/87952120
今日推荐