vue3: Get the current routing address

// 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()What is returned is objectsimilar to that of vue2this.$router

router.currentRouteIt's RefImplan object, that is, we use refthe returned object to .valueaccess the current route, similar to vue'sthis.$route

Use console.logprint out to see

Method 2: window.location  can directly obtain the path of the current window

1.window.location.href (current URL)
        result: http://www.myurl.com:8866/test?id=123&username=xxx
2.window.location.protocol (protocol)
        result: http
3.window.location .host (domain name + port)
        result: www.myurl.com:8866
4.window.location.hostname (domain name)
        result: www.myurl.com
5.window.location.port (port)
        result: 8866
6.window. location.pathname (path part)
        result: /test
7.window.location.search (request parameter)
        result: ?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"
      }
    });
  }

Guess you like

Origin blog.csdn.net/qq_46617584/article/details/126707927