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

Docker installation steps for Redmine

Download the image (optional step, if omitted, it...

Basic use of subqueries in MySQL

Table of contents 1. Subquery definition 2. Subqu...

Detailed tutorial on installing MYSQL under WINDOWS

1. Download the installation package -Choose the ...

How to install JDK and Mysql on Ubuntu 18.04 Linux system

Platform deployment 1. Install JDK step1. Downloa...

About scroll bar in HTML/removing scroll bar

1. The color of the scroll bar under xhtml In the ...

Discussion on horizontal and vertical centering of elements in HTML

When we design a page, we often need to center th...

mySql SQL query operation on statistical quantity

I won't say much nonsense, let's just loo...

What I learned while building my own blog

<br />In one year of blogging, I have person...

How to monitor array changes in Vue

Table of contents Preface Source code Where do I ...

About the problem of vertical centering of img and span in div

As shown below: XML/HTML CodeCopy content to clip...

MySQL slow log online problems and optimization solutions

MySQL slow log is a type of information that MySQ...

MariaDB-server installation of MySQL series

Table of contents Tutorial Series 1. Install Mari...

js canvas to realize the Gobang game

This article shares the specific code of the canv...

MySQL 8.0.20 installation and configuration tutorial under Win10

MySQL 8.0.20 installation and configuration super...