vue.js - 在 Vue 中安装不想给 $route.name

我不知道我是否非常了解 Vue,花了一天时间学习所有 hooks 路线,尝试通过常规 hooks created , mounted它仍然给我null .这是我的组件应用程序,其中插入了所有其他组件。我在这里打电话 mounted钩,所以 基本上它完全渲染了这个组件,我们有一个路径 + 我把 evrythin 包装在 $nextTick 中只是为了确定,它仍然返回我null当前路由器的名称,如果我不是从主 url 而是从 child 访问网站,例如 url/boys。我很久以前就放弃了在我需要它的子组件中获取路由器的这个名称,我想好吧,我将把它作为子组件的路径<the-nav/>那就是我需要它的地方。但它在父级中不起作用,当我进入网站时,我只需要当前路由器的名称。就是这样,听起来很简单,但经历了 hell 。

<template>
      <div id="app">
        <the-header/>
        <the-nav/>
        <div id="app__inner">
          <transition name="fade" mode="out-in">
            <router-view />
          </transition>
        </div>
      </div>
    </template>

    <script>
      import TheHeader from "@/components/TheHeader.vue";
      import TheNav from "@/components/TheNav.vue"

      export default {
        components : {
          TheHeader,
          TheNav
        },
        mounted()
        {
          this.$nextTick(() => {
            console.log(this.$router.currentRoute.name);
          })
        }
      }
    </script>

最佳答案

您实际上可以在这个 befourRouteEnter 函数中使用暴露的路由器函数,像这样使用:


beforeRouteEnter (to, from, next) {
    console.log(to.name);
    next();
  },

请阅读一些关于路由器 Navigation guards 的文档: https://router.vuejs.org/guide/advanced/navigation-guards.html

https://stackoverflow.com/questions/62382218/

相关文章:

javascript - 从用户脚本订阅 Angular 路由器事件

javascript - 类型注释只能在 TypeScript 文件中使用。 TS(8010)

reactjs - Jest 测试在 react 加载中失败

html - 将样式添加到 Bootstrap 表单

ios - iOS 13 的 iOS 多任务 View 中的错误应用程序图标

c++ - 使用 clang 格式时,当行很大时如何将右括号换成新行?

html - 有条件地禁用 JQuery 日期选择器上的星期几?

python - 如何在 PySpark window() 中使用毫秒作为参数。函数之间的范围?

python - ImportError : libX11. so.6: 无法打开共享对象文件: 没

javascript - 无法在 'pipeTo' : Illegal invocation 上执行