vue-router 具有不同参数的相同路由

ser*_*kan 9 vue.js vue-router

我在/entries/12路上。在/entries/13用户单击下一个按钮时,我想在同一个组件上推送。

代码是这样的:

//e is the next page number. 
 this.$router.push({ name: 'Entries', params: { pageNum: e }});
Run Code Online (Sandbox Code Playgroud)

我得到以下错误:

在此处输入图片说明

如果我尝试不同的路线。

全码:

<template>


    <div class="entries">

        <generic-entries @clicked="clicked" ></generic-entries>

    </div>

</template>

<script>
    import GenericEntriesComp from '@/components/GenericEntriesComp';

    export default{
        name: 'entries-comp',

        components: {genericEntries: GenericEntriesComp},
        mounted(){
            var params = {id : this.$route.params.pageNum}
            this.$store.dispatch("getEntries",params);
        },
        methods: {

            clicked(e){

                this.$router.push({ name: 'Entries', params: { pageNum: e.toString() }});

            },
            loadData(){
                var params = {pageNum : this.$route.params.pageNum}
                this.$store.dispatch("getEntries", params)
            }
        },
        computed: {
            items(){
                return this.$store.state.entries
            },
        },
        watch: {
            '$route': 'loadData'
        }
    }


</script>
Run Code Online (Sandbox Code Playgroud)

顺便说一句,错误来自:

Vue.config.errorHandler = function (err, vm, info) {
    console.error(err);
}
Run Code Online (Sandbox Code Playgroud)

Nun*_*iro 29

我用:key. 这样,我保证在 $route 更改时所有路由器视图内容都将重新呈现。

<router-view :key="$route.fullPath" @showLoading="showLoading"></router-view>
Run Code Online (Sandbox Code Playgroud)


ser*_*kan 5

找到了答案。

似乎vue-router按预期工作。就我而言,还有另一个问题。

如果我在通用组件中也有以下代码,我相信它会循环并产生错误。

watch: {
        '$route': function(){
            this.loadData();
        }
    }
Run Code Online (Sandbox Code Playgroud)

就我而言,我从通用组件中删除了观察者并且它起作用了。