Vue-router routing parameters

vue-router provides two forms of parameter passing styles: traditional
style ( ?k0=v0&k1=v1 ), Restful style ( / :key).
queryparams

traditional style

Example URL: http://127.0.0.1:5173/user?userid=123&username=张三
Using the traditional style, src/router/index.jsno adjustments needed, the content is as follows:

import {
    
     createRouter, createWebHashHistory } from 'vue-router'

import Main from '../views/Main.vue'
import User from '../views/User.vue'

import NotFound from '../views/NotFound.vue'

const routes = [
  {
    
    
    path: '/',
    name: 'Main',
    component: Main
  },
  {
    
    
    path: '/user',
    name: 'User',
    component: User,
  },
  {
    
     path: '/:path(.*)', component: NotFound },
]

const router = createRouter({
    
    
  history: createWebHashHistory(),
  routes
})

export default router

Configure routing parameters

// App.vue
<template>
  <router-link to="/user?userid=123&username=张三">go User Page</router-link>
  <router-link :to="{path:'/user', query:{username:'张三', userid:123}}">go User Page</router-link>
  // 推荐
  <router-link :to="{name:'User', query:{username:'张三', userid:123}}">go User Page</router-link>
  <router-view></router-view>
</template>

User.vueGet parameters in

// 组合式
<script setup>
  import {
    
     useRoute } from 'vue-router'
  const route = useRoute()
  const userid = route.query.userid
  const username = route.query.username
</script>

// 选项式
<script>
  export default {
    
    
    data() {
    
    
      return {
    
    
        userid: '',
        username: ''
      }
    },
    mounted() {
    
    
      this.userid = this.$route.query.userid
      this.username = this.$route.query.username
    },
  }
</script>

Restful style parameter passing

Example URL: http://127.0.0.1:5173/user/123/张三
Modify src/router/index.jsas follows:

import {
    
     createRouter, createWebHashHistory } from 'vue-router'

import Main from '../views/Main.vue'
import User from '../views/User.vue'

import NotFound from '../views/NotFound.vue'

const routes = [
  {
    
    
    path: '/',
    name: 'Main',
    component: Main
  },
  {
    
    
    path: '/user/:userid/:username',
    name: 'User',
    component: User,
  },
  {
    
     path: '/:path(.*)', component: NotFound },
]

const router = createRouter({
    
    
  history: createWebHashHistory(),
  routes
})

export default router

Configure routing parameters

// App.vue
<template>
  <router-link to="/user/123/张三">go User Page</router-link>
  // 下面的方式不可用
  // [Vue Router warn]: Path "/user" was passed with params but they will be ignored. Use a named route alongside
  // params instead.
  <router-link :to="{path:'/user', params:{username:'张三', userid:123}}">go User Page</router-link>
  // 推荐
  <router-link :to="{name:'User', params:{username:'张三', userid:123}}">go User Page</router-link>
  <router-view></router-view>
</template>

User.vueGet parameters in

// 组合式
<script setup>
  import {
    
     useRoute } from 'vue-router'
  const route = useRoute()
  const userid = route.params.userid
  const username = route.params.username
</script>

// 选项式
<script>
  export default {
    
    
    data() {
    
    
      return {
    
    
        userid: '',
        username: ''
      }
    },
    mounted() {
    
    
      this.userid = this.$route.params.userid
      this.username = this.$route.params.username
    },
  }
</script>

The above two methods of passing parameters can basically complete the project after learning

expand

  • parameter mapping

The parameters are mapped to by mapping props. By default, only paramsthe parameters in the mapping are implemented as follows:
Modifysrc/router/index.js

const routes = [
  {
    
    
    path: '/',
    name: 'Main',
    component: Main
  },
  {
    
    
    path: '/user/:userid/:username',
    name: 'User',
    component: User,
    // 添加 
    props: ture
  },
  {
    
     path: '/:path(.*)', component: NotFound },
]

After the mapping switch is turned on, it needs propsto be received in the jump page at the same time

// 组合式
<script setup>
  import {
    
     useRoute } from 'vue-router'

  defineProps({
    
    
    userid: String,
    username: String
  })
</script>
//选项式
<script>
  export default {
    
    
    props: {
    
    
      userid: String,
      username: String
    }
  }
</script>

You can also querymap the parameters in , which is a little more troublesome, as follows:

// src/router/index.js
const routes = [
  {
    
    
    path: '/',
    name: 'Main',
    component: Main
  },
  {
    
    
    path: '/user',
    name: 'User',
    component: User,
    // 添加 
    // props: ture
    // 这里我是直接将query 给映射过去了,当然如果你只有一个参数的话,你也可以
    // props: route => ({ userid: route.query.userid })
    props: route => ({
    
     query: route.query })
  },
  {
    
     path: '/:path(.*)', component: NotFound },

The values ​​are as follows:

// User.vue
<template>
  <h2>User Page</h2>
  <div>{
   
   { query.userid }}</div>
  <div>{
   
   { query.username }}</div>
</template>
// 组合式
<script setup>
defineProps({
      
      
  query: {
      
      
    type: Object,
    default() {
      
      
      return {
      
      }
    }
  }
})
</script>
// 选项式
<script>
export default {
      
      
  props: {
      
      
    query: {
      
      
      type: Object,
      default() {
      
      
        return {
      
      };
      }
    }
  }
}
</script>

In addition to the above-mentioned method of passing parameters, you can also use the methods of 共享全局变量, Cookie, localStorageand, interested friends can try

Please refer to https://blog.csdn.net/lwf3115841/article/details/129115106

Guess you like

Origin blog.csdn.net/Twan1234/article/details/129238068