Vue3 gets the current routing address

Vue3 gets the current routing address

Correct answer

Using useRouter :

 // router path: "/user/:uid"
<template>
  <div>user</div>
  <p>uid: {{ uid }}</p>
</template>
 
<script lang="ts">
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 {
      // Returned data uid,
    };
  },
});
</script>

Explain

useRouter() returns object , similar to this.$router in vue2

router.currentRoute is the RefImpl object, which is the object we return using ref . The current route can be accessed through .value , similar to this.$route in Vue.

Use console.log to print it out and see:

This concludes this article about how to get the current routing address in vue3. I hope it will be helpful for everyone’s study, and I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Vue routing vue-router usage explanation
  • Detailed explanation of Vue router routing guard
  • Vue Learning - VueRouter Routing Basics
  • Specific use of routing guards in Vue
  • Vue implements nested routing method example
  • An article to help you understand vue routing

<<:  10 key differences between HTML5 and HTML4

>>:  Clever use of webkit-box-reflect to achieve various dynamic effects (summary)

Recommend

Detailed explanation of the available environment variables in Docker Compose

Several parts of Compose deal with environment va...

Vue echarts realizes horizontal bar chart

This article shares the specific code of vue echa...

How to use default values ​​for variables in SASS

Variables defined in SASS, the value set later wi...

How to use dd command in Linux without destroying the disk

Whether you're trying to salvage data from a ...

Detailed explanation of vue page state persistence

Table of contents Code: Replenish: Summarize Requ...

VMware Workstation download and installation detailed tutorial

Virtual machines are very convenient testing soft...

How to execute PHP scheduled tasks in CentOS7

Preface This article mainly introduces the releva...

js realizes the dynamic loading of data by waterfall flow bottoming out

This article shares with you the specific code of...

Steps to encapsulate the carousel component in vue3.0

Table of contents 1: Encapsulation idea 2. Packag...

Detailed steps for developing Java payment interface for Alipay

Table of contents first step Step 2 Step 3 Step 4...

Two methods to implement MySQL group counting and range aggregation

The first one: normal operation SELECT SUM(ddd) A...