vue-router 2.0 跳转之router.push()
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了vue-router 2.0 跳转之router.push(),小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3185字,纯文字阅读大概需要5分钟。
内容图文
router.push(location)
除了使用 创建 a 标签来定义导航链接,我们还可以借助 router 的实例方法,通过编写代码来实现。
router.push(location)
想要导航到不同的 URL,则使用 router.push 方法。这个方法会向 history 栈添加一个新的记录,所以,当用户点击浏览器后退按钮时,则回到之前的 URL。
当你点击 <router-link> 时,这个方法会在内部调用,所以说,点击 等同于调用 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=2this.$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‘}); }elseif(code === 10){ // 带查询参数,变成/login?stage=stagethis.$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});
eplace
类型: 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(...)
router.push(location) 除了使用 创建 a 标签来定义导航链接,我们还可以借助 router 的实例方法,通过编写代码来实现。 router.push(location) 想要导航到不同的 URL,则使用 router.push 方法。这个方法会向 history 栈添加一个新的记录,所以,当用户点击浏览器后退按钮时,则回到之前的 URL。
当你点击 <router-link> 时,这个方法会在内部调用,所以说,点击 等同于调用 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=2this.$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});123456789101112131415161718192021222324252627282930313233replace 类型: 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(...)--------------------- 作者:愤怒的小青春 来源:CSDN 原文:https://blog.csdn.net/qq_30114149/article/details/78416457 版权声明:本文为博主原创文章,转载请附上博文链接!
原文:https://www.cnblogs.com/nongzihong/p/10281584.html
内容总结
以上是互联网集市为您收集整理的vue-router 2.0 跳转之router.push()全部内容,希望文章能够帮你解决vue-router 2.0 跳转之router.push()所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。