Vue-Router路由Vue-CLI脚手架和模块化开发 之 路由的动态跳转
在上一篇的博文中,实现的跳转是在页面中进行实现的
利用vue-router实例方法,使用js对路由进行动态跳转;
1、router.push:参数为路由对象,跳转到指定路由,跳转后会产生历史记录;
<!--动态跳转的按钮-->
<div>
<button @click="push">push返回首页</button>
</div>
new Vue({
//router : router
router : myRouter, //4 注入路由 简写
methods:{
push(){
myRouter.push({
path:'/home'
})
}
}
}).$mount("#one");

2、router.replace:参数为路由对象,跳转到指定路由,跳转后不产生历史记录;

由效果图可以发现点击replace前往美食广场按扭得时候并不会产生任何的历史记录
<!--动态跳转的按钮-->
<div>
<button @click="push">push返回首页</button>
<button @click="replace">replace前往美食广场</button>
</div>
methods:{
push(){
myRouter.push({
path:'/home'
})
},
replace(){
myRouter.replace({
path:'/foods'
})
}
}
3、router.go:参数为number,number为正向前跳转,为负向后跳转,根据number的值跳转到对应页面,前提是必须有历史记录可供跳转;
4、router.back:无参,后退一个页面,需要有历史记录;
router.forward:无参,前进一个页面,需要有历史记录;

使用的代码:
<!--动态跳转的按钮-->
<div>
<button @click="push">push返回首页</button>
<button @click="replace">replace前往美食广场</button> <button @click="go(-1)">go(-)后退历史记录</button>
<button @click="go(2)">go()前进历史记录</button> <button @click="back">back返回</button>
<button @click="forward">forward前进</button>
</div>
methods:{
push(){
myRouter.push({
path:'/home'
})
},
replace(){
myRouter.replace({
path:'/foods'
})
},
go(n){
myRouter.go(n);
},
back(){
myRouter.back();
},
forward(){
myRouter.forward();
}
}
以上就是通过js实现动态的跳转
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title> 路由的动态跳转</title>
</head>
<body>
<div id="one">
<router-link to="/home">首页</router-link>
<router-link to="/foods">美食</router-link> <div>
<!--将数据显示在这里-->
<router-view></router-view>
</div>
<!--动态跳转的按钮-->
<div>
<button @click="push">push返回首页</button>
<button @click="replace">replace前往美食广场</button> <button @click="go(-1)">go(-)后退历史记录</button>
<button @click="go(2)">go()前进历史记录</button> <button @click="back">back返回</button>
<button @click="forward">forward前进</button>
</div>
</div>
</body>
<template id="foods"> <div> <h2>美食广场</h2>
<ul>
<router-link to="/foods/bjc/北京烤鸭/68" tag="li"> 北京菜</router-link>
<router-link to="/foods/hnc" tag="li"> 湖南菜</router-link>
<router-link to="/foods/xc?name=剁椒鱼头&price=128" tag="li"> 湘菜</router-link>
<router-link :to="ycParam" tag="li"> 粤菜</router-link>
<router-link :to="sccParam" tag="li"> 四川菜</router-link>
</ul> <router-view></router-view>
</div>
</template> <script type="text/javascript" src="../js/vue.js" ></script>
<script type="text/javascript" src="../js/vue-router.js" ></script>
<script> //1 定义组件
let Home = {
template : "<h2>首页</h2>"
}
let Foods = {
template : "#foods",
data(){ return{
sccParam:{ name:'sccRouter', params:{ name:"麻婆豆腐",
price:
}
}, ycParam:{
path:'/foods/yc',
query:{
name:"蜜汁叉烧",
price: } }
}
}
} //定义foods中的子组件 let Bjc={ props:['name','price'],
template : "<h3>北京菜 菜名:{{name}} 价格:{{price}}</h3>" } let Hnc={
template : "<h3>湖南菜 </h3>" }
let Xc={
props:['name','price'],
template : "<h3 >湘菜 菜名:{{name}} 价格:{{price}}</h3>" } let Yc={
props:['name','price'],
template : "<h3>粤菜 菜名:{{name}} 价格:{{price}}</h3>" } let Scc={
props:['name','price'],
template : "<h3>四川菜 菜名:{{name}} 价格:{{price}}</h3>" } //2 配置路由 路由可能有多个
const myRoutes = [
{
path : "/home",
component : Home
},
{
path : "/foods",
component : Foods, children:[
{
path:"bjc/:name/:price",//定义其属性
component:Bjc,
props:true },
{
path:"hnc",
component:Hnc }, {
path:"xc",
component:Xc,
props : (route) => ({
name : route.query.name,
price : route.query.price
}) },
{
path:"yc",
component:Yc,
props:{ name:'蜜汁叉烧量版式',
price:
} },
{
name:'sccRouter',
path:"scc",
component:Scc,
props:true } ]
},
{
path:"*",
redirect:"/home"
}
] // 3 创建路由对象
const myRouter = new VueRouter({
//routes : routes
routes : myRoutes,
//mode:'history'
linkActiveClass : "active" }); new Vue({
//router : router
router : myRouter, //4 注入路由 简写
methods:{
push(){ myRouter.push({ path:'/home'
})
},
replace(){
myRouter.replace({ path:'/foods'
})
}, go(n){ myRouter.go(n);
},
back(){
myRouter.back();
},
forward(){ myRouter.forward();
}
}
}).$mount("#one");
</script>
<style> .active{
color: white; background-color: black;
}
</style>
</html>
路由的动态跳转的总demo
Vue-Router路由Vue-CLI脚手架和模块化开发 之 路由的动态跳转的更多相关文章
- Vue-Router路由 Vue-CLI脚手架和模块化开发 之 路由常用配置与路由嵌套
vue-router路由常用配置 1.mode:配置路由模式,默认为hash,由于URL很丑,可以修改为history,但是需要服务端的支持: 以上一篇的博文为实例: 初始时url的显示: 使用mod ...
- [Vue 牛刀小试]:第十二章 - 使用 Vue Router 实现 Vue 中的前端路由控制
一.前言 前端路由是什么?如果你之前从事的是后端的工作,或者虽然有接触前端,但是并没有使用到单页面应用的话,这个概念对你来说还是会很陌生的.那么,为什么会在单页面应用中存在这么一个概念,以及,前端路由 ...
- Vue Router 常见问题(push报错、push重复路由刷新)
Vue Router 常见问题 用于记录工作遇到的Vue Router bug及常用方案 router.push报错,Avoided redundant navigation to current l ...
- Vue-Router路由 Vue-CLI脚手架和模块化开发 之 使用路由对象获取参数
使用路由对象$route获取参数: 1.params: 参数获取:使用$route.params获取参数: 参数传递: URL传参:例 <route-linke to : "/food ...
- Vue-Router路由Vue-CLI脚手架和模块化开发 之 vue-router路由
vue-router路由:Vue.js官网推出的路由管理器,方便的构建单页应用: 单页应用(SPA)只有一个web页面的应用,用户与应用交互时,动态更新该页面的内容:简单来说,根据不同的url与数据, ...
- Vue-Router路由Vue-CLI脚手架和模块化开发 之 使用props替代路由对象的方式获取参数
在上一章博文中使用路由对象$route获取参数时,组件和路由对象耦合,在这篇博文中就可以使用props来进行解耦: 1.在组件中使用props选项定义数据,接收参数: 2.在路由中,使用props选项 ...
- Vue.js 2.x笔记:路由Vue Router(6)
1. Vue Router简介与安装 1.1 Vue Router简介 Vue Router 是 Vue.js 官方的路由管理器.它和 Vue.js 的核心深度集成,构建单页面应用. Vue Rout ...
- Vue系列:Vue Router 路由梳理
Vue Router 是 Vue.js 官方的路由管理器.它和 Vue.js 的核心深度集成,让构建单页面应用变得易如反掌.包含的功能有: 嵌套的路由/视图表 模块化的.基于组件的路由配置 路由参数. ...
- Vue Router路由管理器介绍
参考博客:https://www.cnblogs.com/avon/p/5943008.html 安装介绍:Vue Router 版本说明 对于 TypeScript 用户来说,vue-router@ ...
随机推荐
- UIBezierPath和CAShapeLayer配合肆意画图
一.CAShapeLayer CAShapeLayer 是 CALayer 的子类,但是比 CALayer 更灵活,可以画出各种图形 使用CAShapeLayer 绘制一个矩形 let layer ...
- 监控和安全运维 1.6 nagios监控客户端-2
6. 继续添加服务服务端 vim /etc/nagios/objects/commands.cfg 增加: define command{ command_name check_nrpe comman ...
- LAMP 2.3 Apache配置防盗链
如果你的站点是一个图片站,有很多非常漂亮的美女图片,那我相信,时间久了会有很多人来你网站借图片,有的人直接下载走了,还有的人直接取走图片的地址,比如你的网站域名是 www.123.com,图片地址为 ...
- python+requests+excel 接口测试
1.EXCEL文件接口保存方式,如图. 2.然后就是读取EXCEL文件中的数据方法,如下: import xlrd class readExcel(object): def __init__(self ...
- CentOS 6.3安装jdk(笔记整理)
1. 下载bin文件,切忌oracle上现在下载到的旧版本的jdk的bin都是网页(执行会报错,见本文最后的截图),他们需要登录oracle后才能下载,所以我这里的url是从googlecode里觅来 ...
- vue插件开发与发布
vue插件的规范 / plug.js Toast={}Toast.install=function(){ Vue.prototype.$toast=function(){ }} // 导出这个对象 e ...
- c# 一维数组,二维数组,多维数组。
数组就是给一个变量定义多个字符,可以是string也可以是int.或者说是一组变量. 可以更加方便的操作大量数据. 数组的定义1.数组里面的内容必须是同一类型2.数据必须有长度限制 一维数组 *一.数 ...
- 用StringBuilder来实现经典的反转问题
import java.util.Scanner; public class Practise03 { public static void main(String[] args) { //键盘录入一 ...
- 1.6 opencv视频操作基础
利用opencv中的VideoCapture类,来对视频进行读取显示,以及调用摄像头. VideoCapture是opencv 2.X中新增的一个类,对应于之前C语言版本的CvCapture结构体.它 ...
- html 连接数据库
http://blog.csdn.net/haxker/article/details/4214001 http://www.cnblogs.com/chuncn/archive/2010/11/22 ...