SoFunction
Updated on 2025-04-05

Example explanation based on routing parameters—simple and easy to understand

In vue, when we build a single page application, we must use vue-router.

vue-router is our route, this plugin provided by vue official

First install vue-router routing dependencies in our project

The first one is provided with the command line to install

npm install vue-router --save

The second type is to download it directly at the official github

/vuejs/vue-router

Routing parameter settings

1. Instantiate a route, and then the address in the route mapping table has parameters. This parameter is the route parameter.

Then set a name value for the route in the mapping table

grammar

{path:"/home/:id,component:home,name:"wang"}

2. When we mount the Vue instance, we want to get the current routing parameters in the home component

Syntax this.$.parameter name

3. Set routing navigation to different paths according to the current parameters

<router-link :to="{name:'wang',params:{id:1}}></router-link>

Here is an example

&lt;!DOCTYPE html&gt;
&lt;html lang="en"&gt;
&lt;head&gt;
 &lt;meta charset="UTF-8"&gt;
 &lt;title&gt;Title&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;div &gt;
 &lt;!--If using dynamic binding,And there isparams You have to add a name to the route--&gt;
 &lt;router-link v-for="(item,index) in article" :key="index" :to="{name:'wang',params:{id:}}" tag="div"&gt;{{}}&lt;/router-link&gt;
 &lt;router-view&gt;&lt;/router-view&gt;
&lt;/div&gt;
&lt;/body&gt;
&lt;script src="node_modules/vue/dist/"&gt;&lt;/script&gt;
&lt;script src="node_modules/vue-router/dist/"&gt;&lt;/script&gt;
&lt;script&gt;
 //Define a component let Article={
  template:"&lt;div&gt;This is the first{{id}}Chapter&lt;/div&gt;",
  computed:{
   id(){
    //Get the parameter value of the routing path    return this.$;
   },
   name(){
    //Get the parameter value of the routing path    return this.$;
   }
  }
 }
 //Route path map table let routes=[
  //:id means that parameters must be passed, but the content can be random  //The value after the current colon and the value passed by the real object will be formed.  // this.$={id:123}
  //{path:'',component:E} //Default route, equivalent to configuring a  //{path:'*',redirect:'/article/:1'} //Default redirect, generally used as a 404 page, will cause path changes  {path:"/article/:id",component:Article,name:'wang'}

 ]
 //Instantiate a route let router=new VueRouter({
  routes
 })
 ('/article/1'); //The first path setting is loaded by default let vm=new Vue({
  el:"#app",
  data:{
   article:[
    {title:"111",id:1},
    {title:"111",id:2},
    {title:"111",id:3}
   ]
  },
  router
 })
&lt;/script&gt;
&lt;/html&gt;

The above example explanation based on routing parameters - simple and easy to understand is all the content I share with you. I hope you can give you a reference and I hope you can support me more.