Two implementation codes of Vue-router programmatic navigation

Two implementation codes of Vue-router programmatic navigation

Two ways to navigate the page

Declarative navigation: The way to achieve navigation by clicking on a link is called declarative navigation, for example: the <a></a> link in a normal web page or <router-link></router-link> in Vue
Programmatic navigation: The method of implementing navigation by calling JavaScript API is called programmatic navigation. For example: location.href in ordinary web pages

Basic usage of programmatic navigation

Commonly used programmatic navigation APIs are as follows:

this.$router.push ('hash address')

this.$router.go(n)

 const User = {  
 		template: '<div><button @click="goRegister">Jump to the registration page</button></div>',  
  	methods: { 
  	 goRegister: function(){   
    // Control route redirection programmatically this.$router.push('/register'); 
  } 
  } 
 }

Specific implementation:

<!DOCTYPE html>
<html lang="en">
 <head>
 <meta charset="UTF-8" />
 <meta name="viewport" content="width=device-width, initial-scale=1.0" />
 <meta http-equiv="X-UA-Compatible" content="ie=edge" />
 <title>Document</title>
 <!-- Import vue file-->
 <!-- <script src="./lib/vue_2.5.22.js"></script> -->
 <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
 <!-- <script src="./lib/vue-router_3.0.2.js"></script> -->
 <script src="https://unpkg.com/vue-router/dist/vue-router.js"></script>
 </head>
 <body>
 <!-- Area controlled by the vm instance-->
 <div id="app">
  <router-link to="/user/1">User1</router-link>
  <router-link to="/user/2">User2</router-link>
  <router-link :to="{ name: 'user', params: {id: 3} }">User3</router-link>
  <router-link to="/register">Register</router-link>

  <!-- Route placeholder -->
  <router-view></router-view>
 </div>

 <script>
  const User = {
  props: ['id', 'uname', 'age'],
  template: `<div>
   <h1>User component -- User id: {{id}} -- Name: {{uname}} -- Age: {{age}}</h1>
   <button @click="goRegister">Go to the registration page</button>
  </div>`,
  methods: {
   goRegister() {
   this.$router.push('/register') //Programmatic navigation}
  },
  }

  const Register = {
  template: `<div>
   <h1>Register Component</h1>
   <button @click="goBack">Go Back</button>
  </div>`,
  methods: {
   goBack() {
   this.$router.go(-1)
   }
  }
  }

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

  // Create vm instance object const vm = new Vue({
  //Specify the controlled area el: '#app',
  data: {},
  //Mount the router instance object// router: router
  router
  })
 </script>
 </body>
</html>

Parameter rules for the router.push() method

 // string (path name) 
router.push('/home') 
// Object router.push({ path: '/home' }) 
// Named routes (passing parameters) 
router.push({ name: '/user', params: { userId: 123 }}) 
// With query parameters, becomes /register?uname=lisi 
router.push({ path: '/register', query: { uname: 'lisi' }})

This is the end of this article about the implementation code of Vue-router programmatic navigation. For more relevant Vue router programmatic navigation content, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Vue router passes parameters and solves the problem of parameter loss when refreshing the page
  • How to use Vue-router routing
  • Vue-router routing lazy loading and 3 ways to implement it
  • Summary of 3 ways to lazy load vue-router
  • Detailed explanation of the difference between hash mode and history mode in Vue-router
  • vue-router defines meta information meta operation
  • Vue router installation and usage analysis
  • Initial practice of vue3.0+vue-router+element-plus
  • How to handle the loss of parameters when refreshing the page when passing parameters to vue router

<<:  Tutorial on installing mysql5.7.18 on windows10

>>:  5 Ways to Clear or Delete Large File Contents in Linux

Recommend

SQL implementation of LeetCode (178. Score ranking)

[LeetCode] 178.Rank Scores Write a SQL query to r...

Detailed explanation of MySql slow query analysis and opening slow query log

I have also been researching MySQL performance op...

JS implements user registration interface function

This article example shares the specific code of ...

8 essential JavaScript code snippets for your project

Table of contents 1. Get the file extension 2. Co...

4 flexible Scss compilation output styles

Many people have been told how to compile from th...

Vue's new partner TypeScript quick start practice record

Table of contents 1. Build using the official sca...

JavaScript implements product details of e-commerce platform

This article shares a common example of viewing p...

Tutorial on how to modify the root password in MySQL 5.7

Version update, the password field in the origina...

File sharing between Ubuntu and Windows under VMware

This article records the method of sharing files ...

MySQL lock control concurrency method

Table of contents Preface 1. Optimistic Locking A...

Alibaba Cloud applies for a free SSL certificate (https) from Cloud Shield

Because the project needs to use https service, I...

A Brief Discussion on the Navigation Window in Iframe Web Pages

A Brief Discussion on the Navigation Window in If...

Detailed explanation of virtual DOM in Vue source code analysis

Why do we need virtual dom? Virtual DOM is design...

Three networking methods and principles of VMware virtual machines (summary)

1. Brigde——Bridge: VMnet0 is used by default 1. P...

Detailed explanation of the use of filter properties in CSS3

Recently, when I was modifying the intranet porta...