簡體   English   中英

在 Vue.js 中檢查您所在頁面的最佳方法

[英]Best way to check what page you're on in Vue.js

我有

當我在汽車詳細信息頁面時,這些簡單的路線/URL

http://localhost:8080/car/1

我正在使用 vue2; 檢查我是否在car頁上的最佳方法是什么?

我一般

會檢查 URL 的第一段,但我不確定這是否是最好的面向未來的方法。

問題

我應該使用JS來檢測我是什么頁面嗎?

我應該使用 Vue 功能來訪問路由器 object 嗎?

為什么一個人會決定選擇一個而不是另一個?

您可以在routes定義中為您的路由提供名稱,例如:

 {
      path: '/car/{id}',
      name: 'car',
      component: CarView
    },

然后使用this.$route.name訪問它,或者您可以解析this.$route.path以使用 String object 方法獲取名稱

也許,嘗試使用: router.currentRoute.path ,其中路由器是:

import Router from "vue-router";

Vue.use(Router);

const routes = [
  { path: "/", component: Home },
  { path: "/test1", component: Test1 },
  { path: "/test2", component: Test2 }
];

const router = new Router({
  routes
});

console.log('Current route: ', router.currentRoute.path);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM