vue2.0 router内watch监听不到route变化?

简单的两个路由,就无法监听到路由变化,不知道怎么回事

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title></title>
</head>
<body>
    <div id="app">
        <router-view></router-view>
    </div>
    <script src="https://cdn.bootcss.com/vue/2.5.17-beta.0/vue.min.js"></script>
    <script src="https://cdn.bootcss.com/vue-router/3.0.1/vue-router.min.js"></script>
    <script>
    var router = new VueRouter({
        routes: [{
            name: 'home',
            component: {
                template: '<div><router-link :to="{name:\'test\'}">test</router-link>&emsp;home</div>',
                watch: {
                    '$route': function(route) {
                        // 监听不到变化??
                        console.log('home', route);
                    }
                }
            },
            path: '/',
        }, {
            name: 'test',
            component: {
                template: '<div><router-link :to="{name:\'home\'}">home</router-link>&emsp;test</div>',
                watch: {
                    '$route': function(route) {
                        // 监听不到变化??
                        console.log('test', route);
                    }
                }
            },
            path: '/test'
        }],
    });
    new Vue({
        el: '#app',
        router: router,
        watch: {
            '$route': function(route) {
                //可以监听到变化
                // console.log('watch', route);
            },
        }
    });
    </script>
</body>

</html>
阅读 10.4k
1 个回答

app组件下不是能正常监听吗
子组件被切换走后就被销毁了,当然监听不到

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
宣传栏