vue更新路由router-view复用组件内容不刷新

本文知识点比较简单,主要面向vue新人解惑,vue前辈请忽略。

 

实现功能:

见上图,这是一个产品列表,当进入不同列表时应该更新内容。

 

代码如下:

//router配置

{
      path: "/products/:category",
      name: "Products",
      components: {
        ...
      }
    }

//组件js配置

mounted() {
        this.getData(this.$route.params.category);
    },
    methods: {
        getData: function(category){
            this.axios.get("/products/" + category).then(res => {

                const data = res.data.data;
                this.pros = data.pro;
            }).catch(error => {
                console.log("error init." + error);
            });
        }
    }

 

目前症状:

1、点击不同类别,url有变化已正确指向不同的路由,但是内容没有更新
2、由当前类别进入其他类别路由后刷新页面,可正确获取数据

 

知识点

在使用Vue-router做项目时,会遇到如/serviceId/:id这样只改变id号的场景。由于router-view是复用的,单纯的改变id号并不会刷新router-view

watch 除了可以监听数据的变化,路由的变化也能被其监听到

:key vue 为你提供了一种方式来声明“这两个元素是完全独立的——不要复用它们”。只需添加一个具有唯一值的 key 属性即可

 

针对以上,经过本人项目实践以及网友贡献,有三种方法可解决:

方法一:通过 watch 监听路由(亲测可行)

mounted() {
	this.getData(this.$route.params.category);
},
methods: {
	getData: function(category){
		...
	}
},
watch: { //通过watch来监听路由变化
	"$route": function(){
		this.getData(this.$route.params.category);
	}
}

 

方法二:用 :key 来阻止“复用”

具体使用方法:

<router-view :key="key"></router-view>
 
computed: {
  key() {
    return this.$route.name !== undefined? this.$route.name +new Date(): this.$route +new Date()
  }
}

tips:使用computed属性和Date()可以保证每一次的key都是不同的,这样就可以如愿刷新数据了

 

方法三:通过 vue-router 的钩子函数 beforeRouteEnter  beforeRouteUpdate  beforeRouteLeave

computed:mapGetters([
   ...
]),
beforeRouteEnter (to, from, next) {
   // 在渲染该组件的对应路由被 confirm 前调用
   // 不!能!获取组件实例 `this`
   // 因为当钩子执行前,组件实例还没被创建
},
beforeRouteUpdate (to, from, next) {
   // 在当前路由改变,但是该组件被复用时调用
   // 举例来说,对于一个带有动态参数的路径 /foo/:id,在 /foo/1 和 /foo/2 之间跳转的时候,
   // 由于会渲染同样的 Foo 组件,因此组件实例会被复用。而这个钩子就会在这个情况下被调用。
   // 可以访问组件实例 `this`
},
beforeRouteLeave (to, from, next) {
   // 导航离开该组件的对应路由时调用
   // 可以访问组件实例 `this`
}

本人尝试使用"beforeRouteUpdate",但位得到解决,其他方法没有尝试。

 

以上内容如果有错误,请各位朋友指出,谢谢。

不,子路由不写<router-view>是不能显示的。在VueRouter中,<router-view>是用来显示匹配到的组件内容的地方。如果没有写<router-view>,则无法将子路由对应的组件内容渲染到页面上。所以,需要在配置子路由时,确保在模板中正确地写上<router-view>。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [总结|VueRouter路由不显示,不报错的问题](https://blog.csdn.net/m0_46635519/article/details/123297388)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [vue2.0 路由不显示router-view的解决方法](https://download.csdn.net/download/weixin_38677244/12762796)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [解决vue更新路由router-view复用组件内容刷新的问题](https://download.csdn.net/download/weixin_38748556/12932708)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值