【Router view】

material

Official Website: https://v3.router.vuejs.org/zh/guide/Silicon
Valley Video: https://www.bilibili.com/video/BV1Zy4y1K7SH?p=118

basic use

Install:

Install vue3.x here

  • Vue 2.x version corresponds to vue-router 3.x
  • Vue 3.x version corresponds to vue-router 4.x
  • other and so on
npm i vue-router@3

configuration file

Define two components: Foo, Bar, used as routing components
insert image description here
Create a new router/index.js file in the src directory, the general content:

// 0. 如果使用模块化机制编程,导入Vue和VueRouter,要调用 Vue.use(VueRouter)
import Vue from "vue";
import VueRouter from "vue-router";
Vue.use(VueRouter);

// 1. 定义 (路由) 组件。
import Foo from "../components/Foo"
import Bar from "../components/Bar"

// 2. 定义路由
const routes = [
    {
    
     path: '/foo', component: Foo },
    {
    
     path: '/bar', component: Bar }
]

// 3. 创建 router 实例,然后传 `routes` 配置
export default new VueRouter({
    
    
    routes // (缩写) 相当于 routes: routes
})

Later, the routing-related components will be placed /pagesunder the path


Modify main.js

main.js add the following content:
insert image description here

use routing

insert image description here

Summarize

insert image description here

multi-level routing

example

Define two components: Bar01 and Bar02
insert image description here
as sub-routes of Bar: (without "/")
insert image description here
use:

  • router-link defines navigation (write full path)
  • Where is the router-view definition displayed?
    insert image description here

Summarize:

insert image description here

routing parameters

query parameter

Note: Use $route.query.xx(be sure to use $route instead of $router) to receive, even if you use string writing, you must use query to receive!
insert image description here

params parameter

insert image description here

insert image description here
insert image description here

props configuration

insert image description here

named route(name)

Premise: You must use the object notation of to, if you use the string notation, it will be treated as a path (path)
insert image description here
insert image description here

properties of router-link

replace

insert image description here

Programmatic Routing Navigation

For example, you want to jump through the following four buttons:
insert image description here
complete push and replace jumps through click events:
insert image description here


Summarize:
insert image description here

Cache routing components:

Use the keep-alive tag to wrap the router-view tag, and use include to specify which components to cache. If not specified, all components displayed in the router-view will be cached. The value of include is the component name, which is this value:
insert image description here


Summary:
insert image description here
If you want to cache more than one, you can use it :include="['name1','name2']"to achieve

Two new lifecycle functions

insert image description here

route guard

global guard

When defining a route, use the following method to identify the permissions required to access the component:

  • meta is the meta information of the route, allowing us to store something in it;
  • isAuth is a variable we saved, of course, other variable names can also be used
    insert image description here

After creating a routing instance, configure the global pre- and post-guards in the following ways:

  • to: where to jump
  • from: from where
  • next: call next() to indicate release
    insert image description here

Key code:
1. Configuration meta:

meta: {
    
    
    isAuth: true
}

2. Configure global guards

//配置全局守卫
router.beforeEach((to, from, next) => {
    
    
    if (to.meta.isAuth) {
    
    
        //权限控制逻辑...
        if (confirm("是否授权")) {
    
    
            next();
        } else {
    
    
            return;
        }
    }
    //放行
    next();
})
router.afterEach((to, from) => {
    
    
    console.log("跳转成功...", to);
})

exclusive guard

When defining a route, use beforeEnter to set an exclusive guard for a certain route: its parameters are exactly the same as the beforeEach parameters of the global guard.
insert image description here

key code:

beforeEnter: (to, from, next) => {
    
    
    //权限控制逻辑...
    if (confirm("是否授权foo02")) {
    
    
        next();
    }
}

Guards within components

Use the following two methods in the component to define the guard in the component

  • beforeRouteEnter: Enter the guard: it is called when entering the component through routing rules
  • beforeRouteLeave: Leave guard: Called when leaving the component through routing rules
    insert image description here

key code:

  beforeRouteEnter(to,from,next){
    
    
    alert("将要通过路由规则进入Bar")
    next()
  },
  
  beforeRouteLeave (to, from, next) {
    
    
    alert("将要通过路由规则离开Bar")
    next()
  }

Summarize

insert image description here
insert image description here
insert image description here

routing mode

Currently, there are three optional values:
insert image description here
official website explanation: https://v3.router.vuejs.org/zh/api/#mode
insert image description here


Summarize:
insert image description here

Guess you like

Origin blog.csdn.net/m0_55155505/article/details/127338918