vue-router  同一个页面地址栏参数改变(比如文章的发布和编辑是同一个页面),不会触发vue的created或者mounted钩子,所以数据不会重新渲染。

解决办法有两种:

1:监听地址栏变化(watch),这是vue-router官方给出的解决办法。

代码

监听路由变化,把初始化的方法重新写到监听的方法里面执行

2、给router-view加个唯一的key,来保证路由切换时都会重新渲染触发钩子了

<template>
    <router-view-anim :key="key"/>
</template>

<script>
    export default {
        name: "index",
        computed:{
            key(){
                return this.$route.name?this.$route.name+new Date():this.$route+ new Date();
            }
        }
    }
</script>
Logo

前往低代码交流专区

更多推荐