微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

【面试题】Vue路由跳转的四种方式用法及区别

Vue路由跳转的四种方式用法及区别

点击打开视频讲解更加详细

<router-link :to="{name:'home'}"> 

<router-link :to="{path:'/home'}"> //name,path都行, 建议用name  

// 注意:router-link中链接如果是'/'开始就是从根路由开始,如果开始不带'/',则从当前路由开始。

二、this.$router.push()

this.$router.push('/home')

this.$router.push({name:'home'})

this.$router.push({path:'/home'})

三、this.$router.replace()

this.$router.replace('/home')

this.$router.replace({name:'home'})

this.$router.replace({path:'/home'})

四、this.$router.go(n)

this.$router.go(n)

向前或者向后跳转n个页面,n可为正整数或负整数

ps : 区别

  • this.$router.push
    跳转到指定url路径,并想history栈中添加一个记录,点击后退会返回到上一个页面
  • this.$router.replace
    跳转到指定url路径,但是history栈中不会有记录,点击返回会跳转到上上个页面 (就是直接替换了当前页面)
  • this.$router.go(n)
    向前或者向后跳转n个页面,n可为正整数或负整数

若对您有帮助,请点击跳转到B站一键三连哦!感谢支持!!!

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐