router.currentRoute 显示的路由与 router.currentRoute.value 不同

发布于 2025-01-13 08:47:20 字数 1115 浏览 1 评论 0 原文

我正在尝试获取组件的当前路由,但 router.currentRoute 显示出一些奇怪的行为。

router.currentRoute 显示预期路由 /admin

RefImpl {__v_isShallow: true, dep: undefined, __v_isRef: true, _rawValue: {…}, _value: {…}}
dep: Set(1) {ReactiveEffect}
__v_isRef: true
__v_isShallow: true
_rawValue: {fullPath: '/admin', path: '/admin', query: {…}, hash: '', name: 'login', …}
_value: {fullPath: '/admin', path: '/admin', query: {…}, hash: '', name: 'login', …}
value: Object
    fullPath: "/admin"
    hash: ""
    href: "/admin"
    matched: [{…}]
    meta: {}
    name: "login"
    params: {}
    path: "/admin"
    query: {}
    redirectedFrom: undefined
    [[Prototype]]: Object
[[Prototype]]: Object

router.currentRoute.value 显示路由 /

{path: '/', name: undefined, params: {…}, query: {…}, hash: '', …}
fullPath: "/"
hash: ""
matched: []
meta: {}
name: undefined
params: {}
path: "/"
query: {}
redirectedFrom: undefined
[[Prototype]]: Object

因此我不能使用应显示当前路由的 router.currentRoute.value.path 。这种行为是预期的吗?如何获取组件当前的路径?

I'm trying to get the current route of a component but router.currentRoute is showing some weird behaviour.

router.currentRoute shows the expected route /admin:

RefImpl {__v_isShallow: true, dep: undefined, __v_isRef: true, _rawValue: {…}, _value: {…}}
dep: Set(1) {ReactiveEffect}
__v_isRef: true
__v_isShallow: true
_rawValue: {fullPath: '/admin', path: '/admin', query: {…}, hash: '', name: 'login', …}
_value: {fullPath: '/admin', path: '/admin', query: {…}, hash: '', name: 'login', …}
value: Object
    fullPath: "/admin"
    hash: ""
    href: "/admin"
    matched: [{…}]
    meta: {}
    name: "login"
    params: {}
    path: "/admin"
    query: {}
    redirectedFrom: undefined
    [[Prototype]]: Object
[[Prototype]]: Object

but router.currentRoute.value shows route /:

{path: '/', name: undefined, params: {…}, query: {…}, hash: '', …}
fullPath: "/"
hash: ""
matched: []
meta: {}
name: undefined
params: {}
path: "/"
query: {}
redirectedFrom: undefined
[[Prototype]]: Object

hence I can't use router.currentRoute.value.path which should show the current route. Is this behaviour expected? How do I get the current route of the component?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

世界如花海般美丽 2025-01-20 08:47:20

从格式来看,您似乎正在使用console.log(或类似)API 来观察代码效果。

请注意,几乎所有现代浏览器在控制台中显示的是对象的“惰性”视图(记录对象时)

检查 此示例(大致遵循 Vue Router 使用的数据结构)

  1. 打开示例
  2. 打开开发工具 - 控制台
  3. 单击按钮两次或多次
  4. 现在仅探索记录的值

结果: < 的每个日志code>router.currentRoute 显示完全相同的值(与 router.currentRoute.value 的日志相反)

现在尝试相同的操作,但在每次单击后展开记录的对象...

TL:DR 不要相信console! - 您看到的值不一定是执行 console.log 时对象的值。

要解决此问题,请使用 console.log(JSON.parse(JSON.stringify(obj))) 代替...

Given by the formatting it seems you are using console.log (or similar) API to observe the code effects.

Note that what almost all modern browsers shows in the console is the "lazy" view of the object (when logging objects)

Check this example (which roughly follows the data structures used by Vue Router)

  1. Open example
  2. Open Dev Tools - Console
  3. Click the button twice or more
  4. Only now explore the logged values

Result: Each log of router.currentRoute shows exactly same value (opposed to logs of router.currentRoute.value)

Now try the same thing but unfold the logged object after each click...

TL:DR Do not trust the console! - the value you see is not necessarily the value of the object at the time console.log was executed.

To workaround the issue use console.log(JSON.parse(JSON.stringify(obj))) instead...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文