Nuxt 获取 URL 的 #hashtag 部分

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

this.$route.path
获取末尾不带哈希码的 URL。如何获取 URL 的哈希部分或获取完整 URL,以便我可以弄清楚如何分离哈希部分?

要明确的是,对于像

https://example.com#test
这样的 URL,我正在 Nuxt 页面中查找获取
test
部分。

似乎无法在

$route
中找到所有内容的文档来查看这是否可用。

javascript vue.js nuxt.js
2个回答
2
投票

您可以通过以下方式实现这一目标

<script>
export default {
  mounted() {
    console.log('mounted', this.$route.hash)
  },
}
</script>

如果是以下 URL:

http://localhost:3000/welcome-home#hello
,它将在控制台打印
#hello

如果你不需要井号,你也可以执行以下操作,只拥有
hello

this.$route.hash.slice(1)

这是这种情况下的

$route
对象

这是有关属性的文档:https://v3.router.vuejs.org/api/#route-object-properties


1
投票

我们可以在这里使用字符串

match()

var url = "https://example.com#test";
var hashtag = url.match(/[^#]+$/)[0];
console.log(hashtag);

© www.soinside.com 2019 - 2024. All rights reserved.