【问题标题】:How to watch router object in main vue instance如何在主 vue 实例中查看路由器对象
【发布时间】:2019-10-07 10:25:56
【问题描述】:

从我的主 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 属性会随着路由的变化而变化(即渲染不同的组件)。

【问题讨论】:

  • @Phil 虽然该帖子的答案是我一直在寻找的,但我的问题直接提到了观看路线对象并且更有可能吸引更多用户。感谢您指向另一篇帖子。我得到了我需要的东西

标签: vuejs2 vue-router watch


【解决方案1】:

观看 $route。

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

【讨论】:

    猜你喜欢
    • 2018-12-01
    • 2020-02-18
    • 1970-01-01
    • 2021-08-25
    • 1970-01-01
    • 2017-05-29
    • 2017-04-11
    • 1970-01-01
    • 2014-12-28
    相关资源
    最近更新 更多