亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

vue3獲取當(dāng)前路由地址的兩種方法

 更新時(shí)間:2022年10月13日 09:28:32   作者:菜雞上線  
近期在做ve3的項(xiàng)目時(shí)因?yàn)楣δ苄枰?需要獲取當(dāng)前路由的地址,下面這篇文章主要給大家介紹了關(guān)于vue3獲取當(dāng)前路由地址的兩種方法,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下

方法一:

// 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 {
      // 返回的數(shù)據(jù)
      uid,
    };
  },
});
</script>

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

router.currentRouteRefImpl對(duì)象, 即我們使用ref返回的對(duì)象, 通過(guò).value可以訪問(wèn)到當(dāng)前的路由, 類似于vue的this.$route

使用console.log打印出來(lái)看看

方式二:window.location 可以直接獲取當(dāng)前窗口的路徑

1.window.location.href(當(dāng)前URL)

        結(jié)果:http://www.myurl.com:8866/test?id=123&username=xxx

2.window.location.protocol(協(xié)議)

        結(jié)果:http

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

        結(jié)果:www.myurl.com:8866

4.window.location.hostname(域名)

        結(jié)果:www.myurl.com

5.window.location.port(端口)

        結(jié)果:8866

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

        結(jié)果:/test

7.window.location.search(請(qǐng)求的參數(shù))

        結(jié)果:?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"
      }
    });
  }

總結(jié)

到此這篇關(guān)于vue3獲取當(dāng)前路由地址的文章就介紹到這了,更多相關(guān)vue3獲取當(dāng)前路由地址內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論