鍍金池/ 問答/HTML/ vue2.0 router內(nèi)watch監(jiān)聽不到route變化?

vue2.0 router內(nèi)watch監(jiān)聽不到route變化?

簡(jiǎn)單的兩個(gè)路由,就無法監(jiān)聽到路由變化,不知道怎么回事

<!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) {
                        // 監(jiān)聽不到變化??
                        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) {
                        // 監(jiān)聽不到變化??
                        console.log('test', route);
                    }
                }
            },
            path: '/test'
        }],
    });
    new Vue({
        el: '#app',
        router: router,
        watch: {
            '$route': function(route) {
                //可以監(jiān)聽到變化
                // console.log('watch', route);
            },
        }
    });
    </script>
</body>

</html>
回答
編輯回答
做不到

app組件下不是能正常監(jiān)聽嗎
子組件被切換走后就被銷毀了,當(dāng)然監(jiān)聽不到

2017年10月29日 17:52