The implementation principle of Vue router-view and router-link

The implementation principle of Vue router-view and router-link

use

<div id="app">
  <router-link to='home'>Home</router-link>
  <router-link to='about'>About</router-link>
  <router-view a=1><router-view/>
 </div>

router-view component

export default {
//Functional components do not have this, cannot be new, and do not have two-way data binding. They are usually used less frequently and are more suitable for displaying detail pages because the detail pages only display and do not modify or perform other operations. Functional components are lighter than stateful components.
  functional:true,
  render(h,{parent,data}){
  parent represents the parent component app
  data is an inline attribute (a=1 in the above code) and can also be passed using prop let route = parent.$route;
    let depth = 0;
    data.routerView = true;
    while(parent){ 
    //$vnode refers to the virtual dom. If there is a virtual dom on the app and the routerView on this virtual dom is true
      if (parent.$vnode && parent.$vnode.data.routerView){
        depth++; 
      }
      parent = parent.$parent;
    }
    let record = route.matched[depth];
    if(!record){
      return h();
    }
    return h(record.component, data);
  }
}

Implementation of router-link

export default {
  props:{
    to:{
      type:String,
      required:true
    },
    tag:{
      type:String
    }
  },
  render(h){
    let tag = this.tag || 'a';
    let handler = ()=>{
      this.$router.push(this.to);
    }
    return <tag onClick={handler}>{this.$slots.default}</tag>
  }
}

This is the end of this article about the implementation principles of Vue router-view and router-link. For more relevant Vue router-view and router-link content, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Nested display implementation of vue router-view
  • Solve the problem that the content of the reused component of vue update router-view does not refresh
  • Tutorial on using router-view component in vue
  • Vue2 sets the default path of router-view
  • Vue implements the method of having multiple router-views on the same page
  • Solution to vue2.0 routing not displaying router-view
  • Solution to router-view not rendering in vue-router
  • Implementation of nested jump of vue routing view router-view

<<:  Python MySQL database table modification and query

>>:  Linux command line quick tips: How to locate a file

Recommend

Vue implements dynamic query rule generation component

1. Dynamic query rules The dynamic query rules ar...

MySQL single table query example detailed explanation

1. Prepare data The following operations will be ...

Nginx memory pool source code analysis

Table of contents Memory Pool Overview 1. nginx d...

Python3.6-MySql insert file path, the solution to lose the backslash

As shown below: As shown above, just replace it. ...

mysql8.0.23 linux (centos7) installation complete and detailed tutorial

Table of contents What is a relational database? ...

Simple implementation method of two-way data binding in js project

Table of contents Preface Publish-Subscriber Patt...

H tags should be used reasonably in web page production

HTML tags have special tags to handle the title of...

Keep-alive multi-level routing cache problem in Vue

Table of contents 1. Problem Description 2. Cause...

MySQL graphical management tool Navicat installation steps

Table of contents Preface 1. Arrange the installa...

The whole process of IDEA integrating docker to deploy springboot project

Table of contents 1. IDEA downloads the docker pl...

How to choose transaction isolation level in MySQL project

introduction Let's start with our content. I ...

Detailed explanation of the simple use of MySQL query cache

Table of contents 1. Implementation process of qu...