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@ ...
随机推荐
- Compare and Swap(CAS)
CAS(Compare and Swap)是个原子操作.拿到一个新值后,CAS将其与内存中的值进行比较,若内存中的值和这个值不一样,则将这个值写入内存,否则,不做操作.在Java的 java.util ...
- 12-01JavaScript事件(Events)
JS事件 1.js事件通常和函数结合来使用,这样可以通过发生的事件来驱动函数的执行,从而引起html出现不同的效果. 2.属性(当这些事件的属性发生时,会触发function{}的函数): 1)ona ...
- 使用pip一次升级所有安装的Python包(太牛了)
import pip from subprocess import call for dist in pip.get_installed_distributions(): call("pip ...
- FFmpeg for Android compiled with x264, libass, fontconfig, freetype and fribidi
android下打算使用ffmpeg的 drawtext ,不过需要 --enable-libfreetype 但是freetype是个第三方库,所以需要先编译freetype,然后再编译ffmpe ...
- 解决列表中增加字典覆盖之前相同key的字典
dic = {} lst = [] # 先声明一个字典和一个列表 dic['name'] = "chenrun" lst.append(dic) print(lst) dic[&q ...
- C++对二进制文件的操作实例
有5个学生的数据,要求: (1)将它们存放到磁盘文件中: (2)将磁盘文件中的第1,3,5个学生数据读入程序,并显示出来: (3)将第三个学生的数据修改后存回磁盘文件中的原有位置: (4)从磁盘文件读 ...
- 华为2013年西安java机试题目:如何过滤掉数组中的非法字符。
这道题目为记忆版本: 题目2描述: 编写一个算法,过滤掉数组中的非法字符,最终只剩下正式字符. 示例:输入数组:“!¥@&HuaWei*&%123” 调用函数后的输出结果,数组:“Hu ...
- 51NOD1835 完全图
传送门 分析 令f(i,j)表示i点完全图有j个联通块的方案数. 讨论有i-1个点已经固定了,我们拉出一个代表元素然后讨论它的集合大小然后组合数算一下就可以了. $$ dp(i,j) = \sum_{ ...
- 什么是MTU,如何检测和设置路由器MTU值
最大传输单元(Maximum Transmission Unit,MTU)是指一种通信协议的某一层上面所能通过的最大数据包大小(以字节为单位).最大传输单元这个参数通常与通信接口有关(网络接口卡.串口 ...
- SDUT 3374 数据结构实验之查找二:平衡二叉树
数据结构实验之查找二:平衡二叉树 Time Limit: 400MS Memory Limit: 65536KB Submit Statistic Problem Description 根据给定的输 ...