Vue3中获取当前路由地址的两种方法,哪种更简便高效?

2026-04-02 07:181阅读0评论SEO资源
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计437个文字,预计阅读时间需要2分钟。

Vue3中获取当前路由地址的两种方法,哪种更简便高效?

目录+方法一:+方式一:+window.location可以直接获取当前窗口的路径+总结+方法一:+// router的path:/user/:uid+template+divuser+/div+puid:{uid}/p+/template+script+import{defineComponent}fromvue;+import{u}fromvue-router;+script+

目录
  • 方法一:
  • 方式二:window.location可以直接获取当前窗口的路径
  • 总结

方法一:

// router的 path: "/user/:uid" <template> <div>user</div> <p>uid: {{ uid }}</p> </template> <script> import { defineComponent } from "vue"; import { useRouter } from "vue-router"; export default defineComponent({ name: "User", setup() { const router = useRouter(); const uid = router.currentRoute.value.params.uid; return { // 返回的数据 uid, }; }, }); </script>

useRouter()返回的是object, 类似于vue2的this.$router

router.currentRouteRefImpl对象, 即我们使用ref返回的对象, 通过.value可以访问到当前的路由, 类似于vue的this.$route

使用console.log打印出来看看

方式二:window.location可以直接获取当前窗口的路径

1.window.location.href(当前URL)

结果:www.myurl.com:8866/test?id=123&username=xxx

2.window.location.protocol(协议)

结果:http

3.window.location.host(域名 + 端口)

结果:www.myurl.com:8866

4.window.location.hostname(域名)

结果:www.myurl.com

5.window.location.port(端口)

结果:8866

6.window.location.pathname(路径部分)

结果:/test

Vue3中获取当前路由地址的两种方法,哪种更简便高效?

7.window.location.search(请求的参数)

结果:?id=123&username=xxx

setup(){ const router = useRouter(); onMounted(() => { console.log("router",router.currentRoute.value) if(window.location.pathname=="/askQuestions"){ // if(router.currentRoute.value.path=="/askQuestions"){ console.log("消失;;;;;;") document.getElementById("navSearch").style.display="none" } }); }

总结

到此这篇关于vue3获取当前路由地址的文章就介绍到这了,更多相关vue3获取当前路由地址内容请搜索易盾网络以前的文章或继续浏览下面的相关文章希望大家以后多多支持易盾网络!

本文共计437个文字,预计阅读时间需要2分钟。

Vue3中获取当前路由地址的两种方法,哪种更简便高效?

目录+方法一:+方式一:+window.location可以直接获取当前窗口的路径+总结+方法一:+// router的path:/user/:uid+template+divuser+/div+puid:{uid}/p+/template+script+import{defineComponent}fromvue;+import{u}fromvue-router;+script+

目录
  • 方法一:
  • 方式二:window.location可以直接获取当前窗口的路径
  • 总结

方法一:

// router的 path: "/user/:uid" <template> <div>user</div> <p>uid: {{ uid }}</p> </template> <script> import { defineComponent } from "vue"; import { useRouter } from "vue-router"; export default defineComponent({ name: "User", setup() { const router = useRouter(); const uid = router.currentRoute.value.params.uid; return { // 返回的数据 uid, }; }, }); </script>

useRouter()返回的是object, 类似于vue2的this.$router

router.currentRouteRefImpl对象, 即我们使用ref返回的对象, 通过.value可以访问到当前的路由, 类似于vue的this.$route

使用console.log打印出来看看

方式二:window.location可以直接获取当前窗口的路径

1.window.location.href(当前URL)

结果:www.myurl.com:8866/test?id=123&username=xxx

2.window.location.protocol(协议)

结果:http

3.window.location.host(域名 + 端口)

结果:www.myurl.com:8866

4.window.location.hostname(域名)

结果:www.myurl.com

5.window.location.port(端口)

结果:8866

6.window.location.pathname(路径部分)

结果:/test

Vue3中获取当前路由地址的两种方法,哪种更简便高效?

7.window.location.search(请求的参数)

结果:?id=123&username=xxx

setup(){ const router = useRouter(); onMounted(() => { console.log("router",router.currentRoute.value) if(window.location.pathname=="/askQuestions"){ // if(router.currentRoute.value.path=="/askQuestions"){ console.log("消失;;;;;;") document.getElementById("navSearch").style.display="none" } }); }

总结

到此这篇关于vue3获取当前路由地址的文章就介绍到这了,更多相关vue3获取当前路由地址内容请搜索易盾网络以前的文章或继续浏览下面的相关文章希望大家以后多多支持易盾网络!