首页 > 解决方案 > 如何在主 vue 实例中查看路由器对象

问题描述

从我的主要 vue 实例中,我试图检测路线何时更改。到目前为止,我了解到我需要深入观察对象以检测属性变化。但我的方法不起作用。我究竟做错了什么:

const app1 = new Vue({
  el: '#app1',
  router,
  data: {
    activeRoute: router.currentRoute
 },

 methods: {
    printclass: function() {
        console.log(router.currentRoute.path);
    }
 },

 computed: {
 },

 watch: {
    'router.currentRoute': {
        handler(newVal) {
            console.log('changed');
        }, deep: true
    }
 },

 created: function() {
    console.log(router.currentRoute);
 }
});

我知道 currentRoute 对象的 path 属性会在路由更改时更改(即呈现不同的组件)。

标签: vuejs2vue-routerwatch

解决方案


观看$路线。

watch: {
     '$route' (to, from) {
        this.yourFunction(           
    },

推荐阅读