如何在主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);
 }
});

我知道,当路由发生变化时,当前Route对象的路径属性会发生变化(即渲染不同的组件(。

观看$路由。

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

最新更新