如何在Nuxt.js中获取当前路由名称?

问题描述 投票:12回答:2

我正在使用Nuxt.js来构建静态网站。

如何访问组件的script代码当前显示的路由名称(我想避免从浏览器位置读取直接URL)?

我可以以某种方式访问​​$route.name

vue.js vuejs2 vue-component vue-router nuxt.js
2个回答
28
投票

是的,你可以使用vuejs路由对象,如$route.name$route.path

$nuxt.$route.path

返回当前路径

$nuxt.$route.name

当前路由的名称(如果有)。

Route Object Properties

路由对象表示当前活动路由的状态。它包含当前URL的解析信息以及URL匹配的路由记录。

  • $ route.path type:string 一个等于当前路径路径的字符串,始终解析为绝对路径。例如“/富/酒吧”。
  • $ route.fullPath type:string 完整解析的URL包括查询和哈希。

**

如果你想得到url params。像这样:enter image description here你这样做:

  data() {
    return {
       zone: this.$nuxt.$route.query.zone,
       jour: this.$nuxt.$route.query.jour

    }   },

**


1
投票

另一种方法是使用以下任一方法:

  • this.$route.pathhttp://localhost:3000的例子,{{this.$route.path}}将打印/
  • this.$route.namehttp://localhost:3000的例子,{{this.$route.name}}将打印index
© www.soinside.com 2019 - 2024. All rights reserved.