Vue2 routing related knowledge points

1. Build routing components


Explain what a route is: a route is a set of mapping relationships (key - value), where the key is a path, and the value may be a function or a component.


If you want to use routing in vue2, you need to install a plugin vue-router.

1.1 Install vue-router


insert image description here

Create corresponding directories and components

insert image description here

1.2 Configure routing


The routes configured in the project are generally stored in the router folder

insert image description here

2. Using routing

Figure 1:

insert image description here


Figure II :

insert image description here

3. Routing jump


The above is to manually switch the route through the url. This is definitely not possible. Users can’t understand these things. Let’s take a look at the two jump modes of the route.

3.1 Declarative Navigation


The first type of routing jump: declarative navigation through router-link label for routing jump

insert image description here

3.2 Programmatic Navigation


The first type of routing jump: programmatic navigation uses push | replace for routing jump.


Regarding what declarative navigation can do, programmatic navigation can also do. Regarding programmatic navigation, in addition to routing jumps, you can also do some other business logic

insert image description here

4. Routing parameters

Regarding routing parameters, there are two types of parameters

  1. params parameter: It belongs to a part of the path. It should be noted that when configuring the route, it needs to occupy a place
  2. query parameter: not part of the path, similar to queryString in ajax, /student?k=v&kv=no need to pass parameters

4.1 Routing passing parameters: string format

Figure 1:

insert image description here

Figure II:

insert image description here


Using template strings

insert image description here


Figure II :
insert image description here


Figure 3:

insert image description here

4.2 Routing passing parameters: object writing

Figure 1:

insert image description here

Figure II :

insert image description here

Figure 3:

insert image description here

4.3 Interview questions related to routing parameters


Topic 1: Can the path be used in combination with the params parameter for routing parameters (object writing)?


Figure 1:

insert image description here


Figure II :

insert image description here

Question 2: How to specify whether the params parameter can be passed or not?


Figure 1:

insert image description here

Topic 3: The params parameter can be passed or not, but how to solve it if the passed is an empty string?

insert image description here


Topic 4: Can routing components pass props data

Figure 1:

insert image description here


Figure II :

insert image description here


Figure 3:

insert image description here

Regarding props passing parameters, in general, it is a simplification of getting params parameters and query parameters.

5. meta usage

Figure 1:

insert image description here

6. Rewrite the push and replace methods


Both push and replace are used by programmatic routing. Here, if the push or replace method is executed multiple times, a warning error will be NavigationDuplicatedthrown .


Demonstrate it:

insert image description here


Figure II:

insert image description here


Let's solve this problem:


Figure 1: A solution that treats the symptoms but not the root cause

insert image description here


Figure 2: Complete solution

insert image description here

In fact, whether this problem is dealt with or not has little impact on the program, but as a programmer, it is unavoidable to feel a little uncomfortable when seeing the red.

Example code:

Guess you like

Origin blog.csdn.net/mu_tong_/article/details/130265565