【问题标题】:How to get current name of a router in Vue?如何在Vue中获取路由器的当前名称?
【发布时间】:2020-10-04 10:29:45
【问题描述】:

在应用程序中包含nav 组件,我想要做的是,如果您不是通过主页访问网站,而是从子页面例如some_url/boys 它在导航中选择该块。所以我在想什么获取路由器名称的最佳方法?我想可能直接在Nav 组件中借助beforeRouteUpdate 钩子获取路由器的名称,但它根本不想输入它。这是我的组件的逻辑

import MenuBar from "@/store/menu.js";

export default {
    namespaced: true,
    name: "TheNav",
    data(){
        return {
            menu: MenuBar.navBar
        }
    },
    beforeRouteUpdate (to, from, next) { // dosn't print anything!
        console.log(1);
        console.log(this.$route.name);
        next()
    },
    methods: {
        ft_click(e, i) // that's animation method, nevermind
        {
            this.menu.forEach( (el) => {
                el.styleNavBar.width = 0 + '%';
                el.active = false;
            })
            this.menu[i].active = true;
            this.menu[i].styleNavBar.width = 80 + '%';
        },
        ft_hover(e, i, width) // that's animation method, nevermind
        {
            if(this.menu[i].active !== true)
            this.menu[i].styleNavBar.width = width + '%';
        }
    }
}

同样的问题,如何在 Vue 中获取路由器的当前名称?或者为什么我的钩子不想工作(((

【问题讨论】:

    标签: vue.js vue-component vue-router


    【解决方案1】:

    this 可以看出,在调用beforeRouteUpdate 时,$route 是否已更新尚不清楚。 beforeRouteUpdate 用于确定批准或阻止next。您可以使用计算或监视属性获取当前名称。

    【讨论】:

    • 我不知道你信不信(this.$router.currentRoute.name); }) } 在 App 组件中仍然返回 null ......
    猜你喜欢
    • 2017-11-28
    • 1970-01-01
    • 1970-01-01
    • 2018-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-09
    • 2019-11-23
    相关资源
    最近更新 更多