this.$route.path
获取末尾不带哈希码的 URL。如何获取 URL 的哈希部分或获取完整 URL,以便我可以弄清楚如何分离哈希部分?
要明确的是,对于像
https://example.com#test
这样的 URL,我正在 Nuxt 页面中查找获取 test
部分。
似乎无法在
$route
中找到所有内容的文档来查看这是否可用。
您可以通过以下方式实现这一目标
<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
我们可以在这里使用字符串
match()
:
var url = "https://example.com#test";
var hashtag = url.match(/[^#]+$/)[0];
console.log(hashtag);