Vue router dynamic routing and routing matching

Vue router dynamic routing matches routing, routing uses regular syntax

1. Dynamic routing

1.1 Create a new user.vue file

In the views folder, create a new user.vue file

<template>
    <div>用户{
    
    {
    
    id}}</div>
</template>

<script setup>
    import {
    
     useRoute } from 'vue-router';
    const id=useRoute().params.id
    console.log("动态路由参数:",id)
</script>

insert image description here

Use to useRoute

1.2 Introduce the user.vue file in the router index.js

Where path is written as: '/user/:id'

insert image description here

1.3 Add entry in App.vue

The path will get 123

insert image description here

insert image description here
will print out

insert image description here

2. Route matching

2.1 404Not Found

insert image description here

    {
    
     path: '/:pathMatch(.*)', component: NotFound }

Where pathMatch fails to match, it will automatically jump to the Notfound page
Use the regular method to match any 404 page

insert image description here

2.2 Regular syntax for other routes

  • path: "/user/:id(\d+)" //The parameter must be a number
  • path: "/user/:id+" //There are multiple parameters +
  • path: "/user/:id*" //parameters are optional*, parameters can be superimposed repeatedly
  • path: "/user/:id?" // parameter optional?, parameters cannot be superimposed repeatedly

Guess you like

Origin blog.csdn.net/Linlietao0587/article/details/128368139