VUE:路由

一、说明

  1)官方提供的用来实现SPA的vue插件

  2)github:https://github.com/vuejs/vue-router

  3)中文文档:http://router.vuejs.org/zh-cn/

  4)下载:npm install vue-router --save

二、相关API

1)VueRouter():用于创建路由器的构建函数  

new VueRouter{{
//多个配置项
}}

2)路由配置:

routes:[
{//一般路由
path:'/about',
component:About
},
{//自动跳转路由
path:'/',
redirect:'/about'
},
]

3)注册路由器:

import router from './router'
new Vue({
router
})

4)使用路由组件标签

  1.<router-link>:用来生成路由链接

    <router-link to='/xxx'>Go to XXX</router-link>

  2.<router-view>:用来显示当前路由组件界面

    <router-view></router-view>

三、基本路由

<template>
<div>
About
</div>
</template> <script>
export default{ }
</script> <style>
</style>

About.vue

<template>
<div>
Home
</div>
</template> <script>
export default{ }
</script> <style>
</style>

Home.vue

<template>
<div>
<div>
<h2>Taosir is studying...</h2></div>
<div>
<router-link to="/about">About</router-link>
<router-link to="/home">Home</router-link>
</div>
<div>
<router-view></router-view>
</div>
</div>
</div>
</div>
</template> <script>
export default { }
</script> <style> </style>

App.vue

import Vue from 'vue'
import App from './App.vue'
import router from './router' new Vue({ //配置对象的属性名都是一些确定的名称,不能随便修改
el: '#app',
router,
components: { App },
template: '<App/>',
router
})

main.js

/*
* 路由器模块
*/
import Vue from 'vue'
import VueRouter from 'vue-router' import About from '../views/About.vue'
import Home from '../views/Home.vue' Vue.use(VueRouter) export default new VueRouter({
//n个路由
routes:[
{
path:'/about',
component:About
},{
path:'/home',
component:Home
},{
path:'/',
redirect:'/about'
}
]
})

index.js

四、嵌套路由

在上面的基础上Home加了一层嵌套

<template>
<div>
<ul>
<li v-for="(news,index) in newsArr" :key="index">{{news}}</li>
</ul>
</div>
</template> <script>
export default{
data(){
return{
newsArr:['news001','news002','news003','news004']
}
}
}
</script> <style>
</style>

News.vue

<template>
<div>
<ul>
<li v-for="(message,index) in messages" :key="message.id">
<a href="???">{{message.title}}</a>
</li>
</ul>
</div>
</template> <script>
export default{
data(){
return {
messages:[]
}
},
mounted(){
//模拟ajax请求从后台获取数据
setTimeout(()=>{
const messages=[
{
id:1,
title:'message001',
},{
id:3,
title:'message003',
},{
id:5,
title:'message005',
}
]
this.messages=messages
},1000)
}
}
</script> <style>
</style>

Message.vue

<template>
<div>
<h2>Home</h2>
<div>
<ul>
<li><router-link to="/home/news">News</router-link></li>
<li><router-link to="/home/message">Message</router-link></li>
</ul>
<div>
<router-view></router-view>
<hr />
</div>
</div>
</div>
</template> <script>
export default{ }
</script> <style>
</style>

Home.vue

/*
* 路由器模块
*/
import Vue from 'vue'
import VueRouter from 'vue-router' import About from '../views/About.vue'
import Home from '../views/Home.vue'
import News from '../views/News.vue'
import Message from '../views/Message.vue' Vue.use(VueRouter) export default new VueRouter({
//n个路由
routes:[
{
path:'/about',
component:About
},{
path:'/home',
component:Home,
children:[
{
path:'/home/news',
component:News
},{
path:'message',
component:Message
},{
path:"",
redirect:"/home/news"
}
]
},{
path:'/',
redirect:'/about'
}
]
})

index.js

    <style>
.router-link-active{
color:red !important;
}
</style>

在index.html加入该样式可以使选中的变红

五、缓存路由的实现

<keep-alive>
<router-view></router-view>
</keep-alive>

六、向路由组件传参

1)通过path

2)通过标签<router-view>

<template>
<div>
<p>ID:{{$route.params.id}}</p>
<ul>
<li>id:{{messageDetail.id}}</li>
<li>title:{{messageDetail.title}}</li>
<li>content:{{messageDetail.content}}</li>
</ul>
</div>
</template> <script>
export default {
data() {
return {
messageDetail: {}
}
},
mounted() {
setTimeout(() => {
const allMessageDetails = [{
id: 1,
title: 'message001',
content: 'message001 content...'
}, {
id: 2,
title: 'message002',
content: 'message002 content...'
}, {
id: 4,
title: 'message004',
content: 'message004 content...'
}]
this.allMessageDetails=allMessageDetails
const id=this.$route.params.id*1
this.messageDetail=allMessageDetails.find(detail=>detail.id===id)
},1000)
},
watch:{
$route:function(value){//路由路径(param)发生了改变
const id=value.params.id*1
this.messageDetail=this.allMessageDetails.find(detail=>detail.id===id)
}
}
}
</script> <style> </style>

MessageDetail.vue

/*
* 路由器模块
*/
import Vue from 'vue'
import VueRouter from 'vue-router' import About from '../views/About.vue'
import Home from '../views/Home.vue'
import News from '../views/News.vue'
import Message from '../views/Message.vue'
import MessageDetail from '../views/MessageDetail.vue' Vue.use(VueRouter) export default new VueRouter({
//n个路由
routes:[
{
path:'/about',
component:About
},{
path:'/home',
component:Home,
children:[
{
path:'/home/news',
component:News
},{
path:'message',
component:Message,
children:[
{
path:'detail/:id',
component:MessageDetail
}
]
},{
path:"",
redirect:"/home/news"
}
]
},{
path:'/',
redirect:'/about'
}
]
})

index.js

<template>
<div>
<ul>
<li v-for="(message,index) in messages" :key="message.id">
<router-link :to="`/home/message/detail/${message.id}`">{{message.title}}</router-link>
</li>
</ul>
<hr />
<router-view></router-view>
</div>
</template> <script>
export default{
data(){
return {
messages:[]
}
},
mounted(){
//模拟ajax请求从后台获取数据
setTimeout(()=>{
const messages=[
{
id:1,
title:'message001',
},{
id:2,
title:'message002',
},{
id:4,
title:'message004',
}
]
this.messages=messages
},1000)
}
}
</script> <style>
</style>

Message.vue

<template>
<div>
<div>
<h2>Taosir is studying...</h2></div>
<div>
<router-link to="/about">About</router-link>
<router-link to="/home">Home</router-link>
</div>
<div>
<router-view msg="abc"></router-view>
</div>
</div>
</div>
</div>
</template> <script>
export default { }
</script> <style> </style>

App.vue

<template>
<div>
<h2>About</h2>
<p>{{msg}}</p>
</div>
</template> <script>
export default{
props:{
msg:String
}
}
</script> <style>
</style>

About.vue

七、编程式路由导航

<template>
<div>
<ul>
<li v-for="(message,index) in messages" :key="message.id">
<router-link :to="`/home/message/detail/${message.id}`">{{message.title}}</router-link>
<button @click="pushShow(message.id)">push查看</button>
<button @click="replaceShow(message.id)">replace查看</button>
</li>
</ul>
<button @click="$router.back()">回退</button>
<hr />
<router-view></router-view>
</div>
</template> <script>
export default{
data(){
return {
messages:[]
}
},
mounted(){
//模拟ajax请求从后台获取数据
setTimeout(()=>{
const messages=[
{
id:1,
title:'message001',
},{
id:2,
title:'message002',
},{
id:4,
title:'message004',
}
]
this.messages=messages
},1000)
}, methods:{
pushShow(id){
this.$router.push(`/home/message/detail/${id}`)
},
replaceShow(id){
this.$router.replace(`/home/message/detail/${id}`)
}
}
}
</script> <style>
</style>

1)this.$router.push(path):相当于点击路由链接(可以返回到当前的路由界面)

2)this.$router.replace(path):用新路由替换当前路由(不可以返回当前的路由界面)

3)this.$router.back():请求(返回)上一个记录路由

4)this.$router.go(1):请求(返回)上一个记录路由

VUE:路由的更多相关文章

  1. Vue路由vue-router

    前面的话 在Web开发中,路由是指根据URL分配到对应的处理程序.对于大多数单页面应用,都推荐使用官方支持的vue-router.Vue-router通过管理URL,实现URL和组件的对应,以及通过U ...

  2. 攻克vue路由

    先下手 路由是个好功能,但是每次都感觉没法开始下手,愣愣的看半天官方文档,所以做个快速开始教程. 首先先搭好HTML文件结构: <!--link和view在一个父元素下--> <di ...

  3. Vue路由学习心得

    GoodBoy and GoodGirl~进来了就看完点个赞再离开,写了这么多也不容易的~ 一.介绍  1.概念:路由其实就是指向的意思,当我们点击home按钮时,页面中就要显示home的内容,点击l ...

  4. VUE路由新页面打开的方法总结

    平常做单页面的场景比较多,所以大部分的业务是在同一个页面进行跳转.要通过VUE路由使用新页面打开且传递参数,可以采用以下两个方法: 1.router-link的target <router-li ...

  5. vue路由参数变化刷新数据

    当路由到某个组件时,由于组件会复用,所以生命周期函数不会再次执行, 如果这个组件是模板组件,靠传入不同数据来显示的.那么,可能会发生参数变化了但页面数据却不变化. 问题 假如有个组件 info.vue ...

  6. 10.3 Vue 路由系统

     Vue 路由系统  简单示例 main.js  import Vue from 'vue' import App from './App.vue' //https://router.vuejs.or ...

  7. vue路由原理剖析

    单页面应用(SPA)的核心之一是: 更新视图而不重新请求页面, 实现这一点主要是两种方式: 1.Hash: 通过改变hash值 2.History: 利用history对象新特性(详情可出门左拐见:  ...

  8. 3种vue路由传参的基本模式

    路由是连接各个页面的桥梁,而参数在其中扮演者异常重要的角色,在一定意义上,决定着两座桥梁是否能够连接成功. 在vue路由中,支持3中传参方式. 场景,点击父组件的li元素跳转到子组件中,并携带参数,便 ...

  9. 14.vue路由&脚手架

    一.vue路由:https://router.vuejs.org/zh/ 1.定义 let router = new VueRouter({ mode:"history/hash" ...

  10. react router @4 和 vue路由 详解(八)vue路由守卫

    完整版:https://www.cnblogs.com/yangyangxxb/p/10066650.html 13.vue路由守卫 a.beforeEach 全局守卫 (每个路由调用前都会触发,根据 ...

随机推荐

  1. 00065字符串缓冲区_StringBuilder类

    1.StringBuilder类,它也是字符串缓冲区,StringBuilder与它和StringBuffer的有什么不同呢? 它一个可变的字符序列.此类提供一个与 StringBuffer 兼容的 ...

  2. Centos与Ubuntu命令

    1.虽然Centos与Ubuntu都是linux的内核,但使用命令还是有所差别 2.如在Centos中跟新插件用的是:yum -y   (yum后面有一个空格) 在Ubuntu中跟新插件用的是:apt ...

  3. CF899A Splitting in Teams

    CF899A Splitting in Teams 题意翻译 n个数,只有1,2,把它们任意分组,和为3的组最多多少 题目描述 There were nn groups of students whi ...

  4. POJ 3709

    简单的单调队列优化,注意是哪些点加入队列即可. #include <iostream> #include <cstdio> #include <algorithm> ...

  5. [Tailwind] Abstract Utility Classes to BEM Components in Tailwind

    When creating UIs with utility classes, a lot of repetition can occur within the HTML markup. In thi ...

  6. asp.net学习指南

    个人总结了一些不错的基础视频教程 视频链接地址(猛戳这里)

  7. Tomcat学习之ClassLoader

    Tomcat学习之ClassLoader 2012-09-04 22:19 8993人阅读 评论(4) 收藏 举报  分类: WEB服务器(13)  版权声明:本文为博主原创文章,未经博主允许不得转载 ...

  8. 数据结构(Java语言)——LinkedList简单实现

    下面是一个能够使用的LinkedList泛型类的实现.这里的链表类名为MyLinkedList,避免与类库中反复. MyLinkedList将作为双链表实现,并且保留到该表两端的引用.这样仅仅要操作发 ...

  9. SQL Server loop - how do I loop through a set of records

    SQL Server loop - how do I loop through a set of records By using T-SQL and cursors like this : DECL ...

  10. ASP.NET Core-组件-后台任务:Hangfire

    ylbtech-ASP.NET Core-组件-后台任务:Hangfire Hangfire作为一款高人气且容易上手的分布式后台执行服务,支持多种数据库.在.net core的环境中,由Core自带的 ...