千峰商城-springboot项目搭建-47-router编程式导航
js代码实现路由跳转:编程式导航。
1.push() (字符串做参数)
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> <script type="text/javascript" src="js/vue.js" ></script> <script type="text/javascript" src="js/vue-router.js"></script> </head> <body> <div id="container"> <button type="button" @click="test">按钮</button> <router-view></router-view> </div> <script type="text/javascript"> const t1 = { template:`<div style="width:400px; height:200px; border:blue 1px solid">index</div>` }; const myrouter = new VueRouter({ routes:[{path:"/a",component:t1,} ]}); var vm = new Vue({ el:"#container", router:myrouter, methods:{ test:function(){ //js代码实现路由跳转:编程式导航 myrouter.push("/a"); } } }); </script> </body> </html>
2.push() (对象做参数)
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> <script type="text/javascript" src="js/vue.js" ></script> <script type="text/javascript" src="js/vue-router.js"></script> </head> <body> <div id="container"> <button type="button" @click="test">按钮</button> <router-view></router-view> </div> <script type="text/javascript"> const t1 = { template:`<div style="width:400px; height:200px; border:blue 1px solid">index</div>` }; const myrouter = new VueRouter({ routes:[{path:"/a",component:t1,} ]}); var vm = new Vue({ el:"#container", router:myrouter, methods:{ test:function(){ //js代码实现路由跳转:编程式导航 //1.字符串做参数 //myrouter.push("/a"); //2.对象做参数 myrouter.push({path:"/a"}); } } }); </script> </body> </html>
3.有参数的路由跳转(name做参数,要指定路由的name)
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> <script type="text/javascript" src="js/vue.js" ></script> <script type="text/javascript" src="js/vue-router.js"></script> </head> <body> <div id="container"> <button type="button" @click="test">按钮</button> <router-view></router-view> </div> <script type="text/javascript"> const t1 = { template:`<div style="width:400px; height:200px; border:blue 1px solid">index:{{$route.params.id}}</div>` }; const myrouter = new VueRouter({ routes:[{path:"/a",name:"r1",component:t1,} ]}); var vm = new Vue({ el:"#container", router:myrouter, methods:{ test:function(){ //js代码实现路由跳转:编程式导航 //1.字符串做参数 //myrouter.push("/a"); //2.对象做参数 //myrouter.push({path:"/a"}); //3.有参数的路由跳转 myrouter.push({name:"r1",params:{id:101}}); } } }); </script> </body> </html>
4.url传值:/a?id=101
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> <script type="text/javascript" src="js/vue.js" ></script> <script type="text/javascript" src="js/vue-router.js"></script> </head> <body> <div id="container"> <button type="button" @click="test">按钮</button> <router-view></router-view> </div> <script type="text/javascript"> const t1 = { template:`<div style="width:400px; height:200px; border:blue 1px solid">index:{{$route.params.id}}</div>` }; const myrouter = new VueRouter({ routes:[{path:"/a",component:t1,} ]}); var vm = new Vue({ el:"#container", router:myrouter, methods:{ test:function(){ //js代码实现路由跳转:编程式导航 //1.字符串做参数 //myrouter.push("/a"); //2.对象做参数 //myrouter.push({path:"/a"}); //3.有参数的路由跳转 //myrouter.push({name:"r1",params:{id:101}}); //4.url传值:/a?id=101 myrouter.push({path:"/a",query:{id:101}}); } } }); </script> </body> </html>
5.不会产生浏览器历史记录的replace()。(功能与push()一致)
myrouter.push("/a");//会产生历史记录
myrouter.replace("/a");//不会产生浏览器历史记录
6.go():参数传递为一个整数,表示在浏览器历史记录中前进或后退多少步。相当于window.history.go(-1)的作用。
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> <script type="text/javascript" src="js/vue.js" ></script> <script type="text/javascript" src="js/vue-router.js"></script> </head> <body> <div id="container"> <button type="button" @click="test">按钮1</button> <button type="button" @click="test2">back</button> <router-view></router-view> </div> <script type="text/javascript"> const t1 = { template:`<div style="width:400px; height:200px; border:blue 1px solid">index:{{$route.params.id}}</div>` }; const myrouter = new VueRouter({ routes:[{path:"/a",component:t1,} ]}); var vm = new Vue({ el:"#container", router:myrouter, methods:{ test:function(){ //js代码实现路由跳转:编程式导航 //1.字符串做参数 myrouter.push("/a");//会产生历史记录 //myrouter.replace("/a");//不会产生浏览器历史记录 //2.对象做参数 //myrouter.push({path:"/a"}); //3.有参数的路由跳转 //myrouter.push({name:"r1",params:{id:101}}); //4.url传值:/a?id=101 //myrouter.push({path:"/a",query:{id:101}}); }, test2:function(){ //在浏览器的历史记录中前进或后退 -1); } } }); </script> </body> </html>
分类:
千峰商城项目
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· PowerShell开发游戏 · 打蜜蜂
· 在鹅厂做java开发是什么体验
· 百万级群聊的设计实践
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战
· 永远不要相信用户的输入:从 SQL 注入攻防看输入验证的重要性