SoFunction
Updated on 2025-04-05

Two implementation codes for Vue-router programming navigation

Two ways to navigate pages

Declarative navigation: The method of navigation by clicking on the link is called declarative navigation
For example: in ordinary web pages<a></a>Link or vue<router-link></router-link>
Programming navigation: by callingJavaScriptFormalAPIThe way to implement navigation is called programmatic navigation
For example: in ordinary web pages

Basic usage of programming navigation

Commonly used programming navigation APIs are as follows:

this.$('hash address')

this.$(n)

 const User = {  
 		template: '&lt;div&gt;&lt;button @click="goRegister"&gt;Jump to the registration page&lt;/button&gt;&lt;/div&gt;',  
  	methods: { 
  	 goRegister: function(){   
    // Use programmatically to control routing jumps    	this.$('/register'); 
  } 
  } 
 } 

Is it specific to implement:

&lt;!DOCTYPE html&gt;
&lt;html lang="en"&gt;
 &lt;head&gt;
 &lt;meta charset="UTF-8" /&gt;
 &lt;meta name="viewport" content="width=device-width, initial-scale=1.0" /&gt;
 &lt;meta http-equiv="X-UA-Compatible" content="ie=edge" /&gt;
 &lt;title&gt;Document&lt;/title&gt;
 &lt;!-- Import vue document --&gt;
 &lt;!-- &lt;script src="./lib/vue_2.5."&gt;&lt;/script&gt; --&gt;
 &lt;script src="/npm/vue/dist/"&gt;&lt;/script&gt;
 &lt;!-- &lt;script src="./lib/vue-router_3.0."&gt;&lt;/script&gt; --&gt;
 &lt;script src="/vue-router/dist/"&gt;&lt;/script&gt;
 &lt;/head&gt;
 &lt;body&gt;
 &lt;!-- quilt vm Areas controlled by instances --&gt;
 &lt;div &gt;
  &lt;router-link to="/user/1"&gt;User1&lt;/router-link&gt;
  &lt;router-link to="/user/2"&gt;User2&lt;/router-link&gt;
  &lt;router-link :to="{ name: 'user', params: {id: 3} }"&gt;User3&lt;/router-link&gt;
  &lt;router-link to="/register"&gt;Register&lt;/router-link&gt;

  &lt;!-- Routing placeholders --&gt;
  &lt;router-view&gt;&lt;/router-view&gt;
 &lt;/div&gt;

 &lt;script&gt;
  const User = {
  props: ['id', 'uname', 'age'],
  template: `&lt;div&gt;
   &lt;h1&gt;User Components -- useridfor: {{id}} -- 姓名for:{{uname}} -- 年龄for:{{age}}&lt;/h1&gt;
   &lt;button @click="goRegister"&gt;Jump to the registration page&lt;/button&gt;
  &lt;/div&gt;`,
  methods: {
   goRegister() {
   this.$('/register')//Programming navigation   }
  },
  }

  const Register = {
  template: `&lt;div&gt;
   &lt;h1&gt;Register Components&lt;/h1&gt;
   &lt;button @click="goBack"&gt;Back&lt;/button&gt;
  &lt;/div&gt;`,
  methods: {
   goBack() {
   this.$(-1)
   }
  }
  }

  // Create a routing instance object  const router = new VueRouter({
  // All routing rules  routes: [
   { path: '/', redirect: '/user' },
   {
   // Name the route   name: 'user',
   path: '/user/:id',
   component: User,
   props: route =&gt; ({ uname: 'zs', age: 20, id:  })
   },
   { path: '/register', component: Register }
  ]
  })

  // Create vm instance object  const vm = new Vue({
  // Specify the control area  el: '#app',
  data: {},
  // Mount the routing instance object  // router: router
  router
  })
 &lt;/script&gt;
 &lt;/body&gt;
&lt;/html&gt;

() Method parameter rules

 // String (path name)('/home') 
// Object({ path: '/home' }) 
// Named route (passing parameters)({ name: '/user', params: { userId: 123 }}) 
// With query parameters, become /register?uname=lisi({ path: '/register', query: { uname: 'lisi' }}) 

This is the end of this article about the implementation code of Vue-router programming navigation. For more related Vue router programming navigation content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!