The use of Vue router is enough

Li gulare 2020-11-12 23:49:39
use vue router


Official website address :https://router.vuejs.org/zh/

Let me introduce myself first , I'm what you call routing , My role is to tell you how to get somewhere , For example, you want to go to a place ( The premise is that this place already exists ) I'll check my roadmap ( Routing configuration ) Tell you how to get there . I see! , My role is to guide you , With my presence , You don't have to memorize every roadmap in detail , Just remember the name of the destination you're going to , As for how to get there , That's my business , You don't have to worry about it , You can get to your destination quickly and accurately according to my instructions .

by the way , I also have three assistants , They are :

1.router : This is my commander , He's in charge of everything route, When you need to show the way , He's going to call all the route, To routes aggregate , And then ask one by one who knows the route , Until we find that route. If you find the one who knows the route route,router Send him to navigate for you , Until we get you to your destination .

2.routes: All the routes route It's all stored here .

3.route: I know by name that I am singular , Limited ability , I can only store one roadmap .

Met three of my assistants , I also have a simple understanding of , Now, let's go to the practice section .

-------- Split line -----------

One :vue-router Primary applications

Final expectation : Click the corresponding connection in the home page to enter the corresponding component .

stay components Create three new components in Namely :A、B、C

 

A:

<template>
<div class="hello">
<ul>
<li>
{
{name}}
</li>
</ul>
</div>
</template>
<script>
export default {
name: 'HelloWorld',
data () {
return {
name: 'my name is A'
}
}
}
</script>
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
</style>

B:

<template>
<div class="hello">
<ul>
<li>
{
{name}}
</li>
</ul>
</div>
</template>
<script>
export default {
name: 'HelloWorld',
data () {
return {
name: 'my name is B!'
}
}
}
</script>
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
h1, h2 {
font-weight: normal;
}
ul {
list-style-type: none;
padding: 0;
}
li {
display: inline-block;
margin: 0 10px;
}
a {
color: #42b983;
}
</style>

C:

<template>
<div class="hello">
<ul>
<li>
{
{name}}
</li>
</ul>
</div>
</template>
<script>
export default {
name: 'HelloWorld',
data () {
return {
name: 'my name is C!'
}
}
}
</script>
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
h1, h2 {
font-weight: normal;
}
ul {
list-style-type: none;
padding: 0;
}
li {
display: inline-block;
margin: 0 10px;
}
a {
color: #42b983;
}
</style>

Configure the routing :

/router/index.js

import Vue from 'vue'
import Router from 'vue-router'
import HelloWorld from '@/components/HelloWorld'
import A from '@/components/A'
import B from '@/components/B'
import C from '@/components/C'
Vue.use(Router)
export default new Router({
routes: [
{
path: '/',
name: 'HelloWorld',
component: HelloWorld
},
{
path: '/A',
name: 'A',
component: A
},
{
path: '/B',
name: 'B',
component: B
},
{
path: '/C',
name: 'C',
component: C
}
]
})

stay HelloWord.vue Introduction in A、B、C The connection of the three components 、

<template>
<div class="hello">
<ul>
<li>
<a href="/#/A">A</a>
</li>
<li>
<a href="/#/B">B</a>
</li>
<li>
<a href="/#/C">C</a>
</li>
</ul>
</div>
</template>
<script>
export default {
name: 'HelloWorld',
data () {
return {
msg: 'Welcome to Your Vue.js App'
}
}
}
</script>
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
h1, h2 {
font-weight: normal;
}
ul {
list-style-type: none;
padding: 0;
}
li {
display: inline-block;
margin: 0 10px;
}
a {
color: #42b983;
}
</style>

Run the command :npm run dev

Open the website and run it :

 

Come here vue-router This is the end of the article , Sum up , There are three parts of knowledge

1. Define three components A、B、C

2. stay router/index.js The file introduces three components , And do the corresponding routing configuration

3. Add... To the page 3 individual a label , Jump to the corresponding component respectively

This part of the content is relatively elementary , It's just a simple demonstration of routing , Enter the corresponding component through a defined route , In our normal development , The requirements involved are much more complex than that , The following content will gradually deepen the difficulty , Now let's talk about vue-router An advanced version of :vue-router Intermediate application , This part is divided into three sections :

1. How to dynamically define routing

2. How to pass parameters in routing

In the dynamic route setting, the purpose of passing parameters is also achieved , But it's not the right posture to transfer parameters , It's just a path in url node . It is limited to pass parameters in this way .
Let's talk about another way to pass parameters , Before introducing this approach , You need to understand a component first :router-link
router-link That's the right way to add navigation links . Someone might say , I'll use it directly <a></a> Isn't it the same effect , And it's easier to write about .
no , It must be unknown that we can mention this point router-link The default rendering result is <a></a>
<router-link to="/link">link</router-link>
Render the result
<a data-v-acf16c92="" href="#/link" class="">link</a>
Is it right to know this router-link It's even better , Let's see what other properties are better ?
to: The connection to be reached , There are two kinds of acceptable parameters string、object, And this attribute must be added .

If you don't write this property :


When using string Type , This string Value is the path , There's nothing to say , Let's focus on when the parameter is object when ,router-link How to render , meanwhile , The parameter is also here object Written in .
Write 2 An example ,
<router-link :to="{ name: 'link', params: { myName: ' Liguulare ' }}">User</router-link>
<!-- Render the result -->
<a data-v-acf16c92="" href="#/" class="router-link-active">User</a>

<router-link :to="{ path: '/link', query: { myName: ' Liguulare ' }}">User</router-link>
<!-- Render the result -->
<a data-v-acf16c92="" href="#/link?myName=%E6%9D%8E%E5%8F%A4%E6%8B%89%E9%9B%B7" class="">User</a>
The two writing methods render two different ways of transmitting parameters , It looks like a kind of post The ginseng , One is get The ginseng .
But the two ways accept parameters in the same way
The first one is :this.$router.params.myName
The second kind :this.$router.query.myName

tag: This attribute is used to decide what label to render , Because the default is a, therefore router-link The default rendering is <a></a> label , If I put tag Set to li, Let's see what it's going to render ,
<router-link tag="li">User</router-link>
<!-- Render the result -->
<li data-v-acf16c92="" class="">User</li>
It's just one. li label , But it also listens for click events , Trigger route navigation .
There are other attributes that are better completed , however , It's not the scope of this section , We only introduce how to pass parameters here , If you are interested, go to :https://router.vuejs.org/zh/api/#router-link-props

3. What's the use of route naming

 

版权声明
本文为[Li gulare]所创,转载请带上原文链接,感谢

  1. [front end -- JavaScript] knowledge point (IV) -- memory leakage in the project (I)
  2. This mechanism in JS
  3. Vue 3.0 source code learning 1 --- rendering process of components
  4. Learning the realization of canvas and simple drawing
  5. gin里获取http请求过来的参数
  6. vue3的新特性
  7. Get the parameters from HTTP request in gin
  8. New features of vue3
  9. vue-cli 引入腾讯地图(最新 api,rocketmq原理面试
  10. Vue 学习笔记(3,免费Java高级工程师学习资源
  11. Vue 学习笔记(2,Java编程视频教程
  12. Vue cli introduces Tencent maps (the latest API, rocketmq)
  13. Vue learning notes (3, free Java senior engineer learning resources)
  14. Vue learning notes (2, Java programming video tutorial)
  15. 【Vue】—props属性
  16. 【Vue】—创建组件
  17. [Vue] - props attribute
  18. [Vue] - create component
  19. 浅谈vue响应式原理及发布订阅模式和观察者模式
  20. On Vue responsive principle, publish subscribe mode and observer mode
  21. 浅谈vue响应式原理及发布订阅模式和观察者模式
  22. On Vue responsive principle, publish subscribe mode and observer mode
  23. Xiaobai can understand it. It only takes 4 steps to solve the problem of Vue keep alive cache component
  24. Publish, subscribe and observer of design patterns
  25. Summary of common content added in ES6 + (II)
  26. No.8 Vue element admin learning (III) vuex learning and login method analysis
  27. Write a mini webpack project construction tool
  28. Shopping cart (front-end static page preparation)
  29. Introduction to the fluent platform
  30. Webpack5 cache
  31. The difference between drop-down box select option and datalist
  32. CSS review (III)
  33. Node.js学习笔记【七】
  34. Node.js learning notes [VII]
  35. Vue Router根据后台数据加载不同的组件(思考-&gt;实现-&gt;不止于实现)
  36. Vue router loads different components according to background data (thinking - & gt; Implementation - & gt; (more than implementation)
  37. 【JQuery框架,Java编程教程视频下载
  38. [jQuery framework, Java programming tutorial video download
  39. Vue Router根据后台数据加载不同的组件(思考-&gt;实现-&gt;不止于实现)
  40. Vue router loads different components according to background data (thinking - & gt; Implementation - & gt; (more than implementation)
  41. 【Vue,阿里P8大佬亲自教你
  42. 【Vue基础知识总结 5,字节跳动算法工程师面试经验
  43. [Vue, Ali P8 teaches you personally
  44. [Vue basic knowledge summary 5. Interview experience of byte beating Algorithm Engineer
  45. 【问题记录】- 谷歌浏览器 Html生成PDF
  46. [problem record] - PDF generated by Google browser HTML
  47. 【问题记录】- 谷歌浏览器 Html生成PDF
  48. [problem record] - PDF generated by Google browser HTML
  49. 【JavaScript】查漏补缺 —数组中reduce()方法
  50. [JavaScript] leak checking and defect filling - reduce() method in array
  51. 【重识 HTML (3),350道Java面试真题分享
  52. 【重识 HTML (2),Java并发编程必会的多线程你竟然还不会
  53. 【重识 HTML (1),二本Java小菜鸟4面字节跳动被秒成渣渣
  54. [re recognize HTML (3) and share 350 real Java interview questions
  55. [re recognize HTML (2). Multithreading is a must for Java Concurrent Programming. How dare you not
  56. [re recognize HTML (1), two Java rookies' 4-sided bytes beat and become slag in seconds
  57. 【重识 HTML ,nginx面试题阿里
  58. 【重识 HTML (4),ELK原来这么简单
  59. [re recognize HTML, nginx interview questions]
  60. [re recognize HTML (4). Elk is so simple