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

Things about installing Homebrew on Mac

Recently, Xiao Ming just bought a new Mac and wan...

How to uninstall MySQL 5.7.19 under Linux

1. Find out whether MySQL was installed before Co...

How to create a new user in CentOS and enable key login

Table of contents Create a new user Authorize new...

Detailed explanation of MySQL file storage

What is a file system We know that storage engine...

A brief analysis of MySQL backup and recovery

Table of contents 1. Introduction 2. Simple defin...

mysql create database, add users, user authorization practical method

1. Create a MySQL database 1. Create database syn...

11 ways to remove duplicates from js arrays

In actual work or interviews, we often encounter ...

Summarize how to optimize Nginx performance under high concurrency

Table of contents Features Advantages Installatio...

Angular environment construction and simple experience summary

Introduction to Angular Angular is an open source...

MySQL independent index and joint index selection

There is often a lack of understanding of multi-c...

Analysis of MySQL lock mechanism and usage

This article uses examples to illustrate the MySQ...