VUE的路由器的总结
vue的路由器,我们在使用vue进行开发的时候,是必须用到的一个vue自带的组件,下面进行vue经常的操作的一些说明
1.vue-router的安装
在命令行里面使用 cnpm install vue-router --save命令进行安装
在main.js或者其他使用vue-router的地方,通过import来导入 import VRouter from 'vue-router'
//使用vue.use来注册使用VRouter插件
Vue.use(VRouter);
import Vue from 'vue'
import App from './App'
import VRouter from 'vue-router'
import Apple from './components/apple'
import Banana from './components/banana'
import Redapple from './components/redapple'
//使用vue.use来注册使用VRouter插件
Vue.use(VRouter);
//这样VRouter作为一个类来使用,我们自己实例化这样的一个类
let router = new VRouter({
mode: 'history',
routes: [
{
path: '/apple/:color',//为页面设置路由参数
component: Apple,
//路由嵌套
children: [
{
path: 'redapple',
component: Redapple 注意:在路径上加上‘/’表示从根目录开始跳转
不加‘/’表示从当前页面进行跳转
}
]
},
{
path: '/banana',
component: Banana
}
]
});
/* eslint-disable no-new */
new Vue({
el: '#app',
router,//将这样的router放到根目录里面
// es6 通过render方法来使用外部引入的App组件
render: h => h(App)
})
在上面这样使用了之后,在我们所需要的页面进行展示
<!-- v-router的使用 -->
<router-view></router-view> //展示
<router-link :to="'apple'"> to apple</router-link> //相当于a标签
<router-link :to="{path:'/banana',query:{color:'yellow'}}">to banana</router-link>
<router-link :to="{path:'apple/redapple'}">to applered</router-link> 注意:除了根据path以外,我们还可以为这个路由跳转设置名字,也可以通过名字来进行路由的跳转
<router-link :to="{name:'myba'}">name router</router-link>
{path: '/banana',
name: 'myba',
component: Banana}
需要特别注意的是,我们在使用v-bind进行路由的路径的绑定的时候,有两种方式,一个是直接以对象的方式进行绑定,类似于
<router-link :to="{path:'/banana',query:{color:'yellow'}}">to banana</router-link>
另外一种就是通过绑定字符串,但是这个字符串必须在data()里面进行设置,类似于
<router-link :to="'apple'"> to apple</router-link>
data () {
return {
apple: '/apple', } }
VUE带参数的路由跳转
我们在使用vue的路由器的时候,有的时候会需要传递参数,目前我所接触的有两种方式的跳转,一个是在router的配置文件当中进行参数的配置,类似于
let router = new VRouter({
mode: 'history',
routes: [
{
path: '/apple/:color',//为页面设置路由参数
component: Apple,
//路由嵌套
children: [
{
path: 'redapple',
component: Redapple
}
]
},
{
path: '/banana',
component: Banana
}
]
});
然后在apple.vue这个页面通过$route.params进行获取到所传递的参数
<template>
<div>
{{ msg }}
<p>{{ $route.params.color }}</p>
<button @click="getparams">get params</button>
<router-view></router-view>
<router-link :to="{path:'apple/redapple'}">to red apple</router-link>
</div>
</template>
<script>
export default {
data () {
return {
msg: 'i am apple'
}
},
methods: {
getparams () {
console.log(this.$route.params);
}
}
}
另外一种是直接在页面当中<router-link></router-link>里面通过query进行参数的绑定
<router-link :to="{path:'/banana',query:{color:'yellow'}}">to banana</router-link>
<template>
<div>
{{ msg }}
{{$route.query.color}}
<button @click="getparams">get params</button>
</div>
</template>
<script>
export default {
data () {
return {
msg: 'i am banana'
}
},
methods: {
getparams () {
console.log(this.$route.query.color);
}
}
}
</script>
他们之间的区别我认为就是一个是需要自己去在后面加上/参数,http://localhost:8080/apple/apple
一个是点击了之后直接在地址后面加上hash, http://localhost:8080/banana?color=yellow(这种的比较好接受); VUE的地址重定向
我们在网站当中往往是http://localhost:8080访问的是app.vue的界面,这个时候我们想让我们的网站打开的时候,直接是apple.vue的界面,这个时候我们就用到了vue的地址重定向的功能,
我们需要在routers的配置里面加上这个就可以了
et router = new VRouter({
mode: 'history', //去掉地址栏中默认的#hash
routes: [
{
path: '/',//表示根目录
redirect: '/banana' //表示你要重定向的位置
},
{
path: '/banana',
component: Banana
}
}
其他的使用方法暂时还没有用到,后期用到了之后会更新
关于这个带路由参数的跳转,参考了 http://www.jb51.net/article/114432.htm
router.push(location)
除了使用 <router-link> 创建 a 标签来定义导航链接,我们还可以借助 router 的实例方法,通过编写代码来实现。
router.push(location)
想要导航到不同的 URL,则使用 router.push 方法。这个方法会向 history 栈添加一个新的记录,所以,当用户点击浏览器后退按钮时,则回到之前的 URL。
当你点击 <router-link> 时,这个方法会在内部调用,所以说,点击 <router-link :to="..."> 等同于调用 router.push(...)。
声明式:<router-link :to="...">
编程式:router.push(...)
该方法的参数可以是一个字符串路径,或者一个描述地址的对象。
// 字符串
router.push('home')
// 对象
this.$router.push({path: '/login?url=' + this.$route.path});
// 命名的路由
router.push({ name: 'user', params: { userId: 123 }})
// 带查询参数,变成/backend/order?selected=2
this.$router.push({path: '/backend/order', query: {selected: "2"}});
// 设置查询参数
this.$http.post('v1/user/select-stage', {stage: stage})
.then(({data: {code, content}}) => {
if (code === 0) {
// 对象
this.$router.push({path: '/home'});
}else if(code === 10){
// 带查询参数,变成/login?stage=stage
this.$router.push({path: '/login', query:{stage: stage}});
}
});
// 设计查询参数对象
let queryData = {};
if (this.$route.query.stage) {
queryData.stage = this.$route.query.stage;
}
if (this.$route.query.url) {
queryData.url = this.$route.query.url;
}
this.$router.push({path: '/my/profile', query: queryData});
replace
类型: boolean
默认值: false
设置 replace 属性的话,当点击时,会调用 router.replace() 而不是 router.push(),于是导航后不会留下 history 记录。即使点击返回按钮也不会回到这个页面。
//加上replace: true后,它不会向 history 添加新记录,而是跟它的方法名一样 —— 替换掉当前的 history 记录。
this.$router.push({path: '/home', replace: true})
//如果是声明式就是像下面这样写:
<router-link :to="..." replace></router-link>
// 编程式:
router.replace(...)
综合案例
this.$router.push({path: '/coach/' + this.$route.params.id, query: queryData});
vue2.0 $router和$route的区别
做项目的时候发现传参数是通过 this.$route.query或者 this.$route.params接收router-link传的参数。
在路由跳转的时候除了用router-link标签以外需要在script标签在事件里面跳转,所以有个方法就是在script标签里面写this.$router.push('要跳转的路径名'),
在写的时候发现这两个为什么不同,在控制台打出this的时候,发现$route和$router同时存在
$route为当前router跳转对象里面可以获取name、path、query、params等
$router为VueRouter实例,想要导航到不同URL,则使用$router.push方法
返回上一个history也是使用$router.go方法
2.路由使用的部分技巧
(1)我们在路由的跳转当中,可以使用watch来监听路由的跳转变化
watch: {
changeShowType(value) {
console.log("-----"+value);
},
'$route'(to,from){
console.log(to); //去往的界面
console.log(from); //来自的界面
if(to.path=="/shop/detail"){
console.log("商品详情");
}
}
},
(2) 我们通常会在页面当中使用到page404的界面,这个时候的路由设置为
{
path: '*', //这个时候的路径就是*
component: page404
}
注意:这个配置必须放置在路由表的最后,因为*代表所有的,找到*之后下面的不会再去执行了

yyzz因为在路由的配置当中不存在,所以进入的界面为预先设置好的404界面

(3)路由当中mode方式的history和hash的区别
我们知道普通的jquery的跳转界面就是以hash的方式进行,所以每一个地址栏当中会出现默认的#号,例如

而在vue当中我们可以用history来模仿url的连接跳转

附上 history和hsah的区别网址https://www.cnblogs.com/photon-phalanx/p/7452331.html
(3)路由钩子函数
简单的理解钩子函数其实就是告诉服务器客户端正在做什么,比如说,客户端现在要跳转链接到界面,客户端跳转之前,先发起请求告诉服务器我可不可以进行跳转,这个时候服务器必须给到响应告诉客户端说是否可以进行跳转
路由的钩子函数常用到的两种形式:一个是直接写在路由的js当中,一个是写到要跳转的那个vue当中
1.直接写在路由js当中的路由钩子函数(直接写在路由表里面的beforeEnter就不存在afterEnter了)

next()表示可以跳转,不能跳转就用next(false)
我们也可以使用next({path:'/st'})来使路由跳转到你自定义的链接上去
2.路由的钩子函数也可以写在我们将要跳转的vue当中(这个时候就可以同时拥有Enter和Leave了)

methods: {
},
components: {
headTit
},
beforeRouteEnter(to, from, next) {
console.log("可不可以进行跳转");
next();
},
beforeRouteLeave(to, from, next) {
console.log("可不可以跳走");
next();
}
//注意在beforeRouteLeave当中是没有next({path})这种方式的,只有在Enter的时候才会有,但是同样的Enter里面没有this
要想离开的时候跳转到别的界面,可以使用this.$router.push的方式或者进行路由判断的形式
beforeRouteLeave(to, from, next) {
console.log("离开详情");
next();
this.$router.push('/uououiu');
}
beforeRouteLeave(to, from, next) {
console.log(to);
if(to.path=='/shop/yyzz'){
next({path:'/shop'});
}
next();
//this.$router.push('/uououiu');
}
VUE的路由器的总结的更多相关文章
- Vue复杂路由器的实现
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
- Vue路由器的hash和history两种工作模式 && Vue项目编译部署
1 # 一.Vue路由器的两种工作模式 2 # 1.对于一个uri来说,什么是hash值? 井号及其后面的内容就是hash值. 3 # 2.hash值不会包括含在HTTP请求中,即:hash值不会带给 ...
- Vue 全家桶
第 1 章:Vue 核心 1.1. Vue 的基本认识1.1.1. 官网1) 英文官网: https://vuejs.org/2) 中文官网: https://cn.vuejs.org/ 1.1.2. ...
- vue 快速入门 系列 —— vue-router
其他章节请看: vue 快速入门 系列 Vue Router Vue Router 是 Vue.js 官方的路由管理器.它和 Vue.js 的核心深度集成,让构建单页面应用变得易如反掌. 什么是路由 ...
- Vue系列教程(三)之vue-cli脚手架的使用
一.Vue-cli的环境准备 目的:(1)快速管理依赖 (2)确定项目结构 1.安装node.js Node.js是一个可以让前端运行在服务器上的一个工. 下载:https://nodejs.org/ ...
- Vue路由器的简单实现
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
- 做一个gulp+webpack+vue的单页应用开发架子
1.目标 最近项目上的事情不多,根据我自己的开发习惯,决定开发一些简单的开发架子,方便以后事情多的时候直接套用.本文讲的一个gulp+webpack+vue的单页应用架子,想要达到的目的: 可以通过命 ...
- Vue.js——vue-router 60分钟快速入门
概述 vue-router是Vue.js官方的路由插件,它和vue.js是深度集成的,适合用于构建单页面应用.vue的单页面应用是基于路由和组件的,路由用于设定访问路径,并将路径和组件映射起来.传统的 ...
- vue小总结
以下是我在使用vue过程中自己对vue的一些小总结,希望对学习vue的亲们能有所帮助: 1. http的post请求: this.$http({url: '/someUrl', method: ' ...
随机推荐
- 关于Windows自动化卸载软件的思路
思路 关于控制面板“卸载”关联到的exe是这样的: 注册表:HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Uninstall ...
- OBS录制全屏游戏的方法(超好录屏)
新版Windows设置 详见 https://github.com/obsproject/obs-studio/wiki/Laptop-Troubleshooting 新版的Windows 10: l ...
- C++版本 ORM 访问数据库之ODB 的oracle Demo测试(二)
有上篇文章已经说了odb的环境编译, 现在直接拿来给的例子进行数据库的增删改查操作测试 1. ODB访问oracle数据库_ 插入操作(insert) 直接运行上篇编译好的exe文件会出现如下错误 错 ...
- GreenPlum 大数据平台--segment 失效问题排查
01,segment 检查一: 在master节点上检查失效的segment 正常情况下: :::: gpstate:greenplum01:gpadmin-[INFO]:-Starting gpst ...
- Jquery表单插件使用
一般表单提交都是同步,可以使用$.post进行异步提交,但这样意味着当表单属性很多的时候,要写的js也很多($("#xxx").val()获取属性的值后,在放入$.post第二个参 ...
- Loj #3085. 「GXOI / GZOI2019」特技飞行
Loj #3085. 「GXOI / GZOI2019」特技飞行 题目描述 公元 \(9012\) 年,Z 市的航空基地计划举行一场特技飞行表演.表演的场地可以看作一个二维平面直角坐标系,其中横坐标代 ...
- 查看linux中某个端口port是否被利用
(1)lsof -i:端口号查看某个端口是否被占用 (2)netstat -an|grep 80 netstat -- show network status (3)杀掉进程 kill pid 注意: ...
- Oracle Hint用法整理笔记
目录 1./+ result_cache / 2./+ connect_by_filtering / 3./+ no_unnset / 4./+ index(表别名 索引名) / 5./+ INDEX ...
- Java并发专栏(一)—— Process vs Thread
一.前言 程序是代码和数据的集合,是一种静态实体.不具有代码执行和数据处理的能力,更多是一种行为的描述. 如果将程序和处理器结合,处理器将程序加载至内存,然后执行程序代码处理数据.这时就是可执行的程序 ...
- 基于.NET平台常用的框架整理 转自 http://www.cnblogs.com/zhuyongblogs/p/5353751.html
常用的一些开源组件整理: 导出Excel报表的插件:NOPI.dll(基于微软OpenXml实现)开源的作业调度和自动任务框架:Quartz.NET用于大数据搜索引擎的全文检索框架:Lucene.ne ...