Vue 路由跳转、返回某一页方式

11 篇文章 0 订阅

1、映射路由 <router-link> 标签  to属性


<!-- path跳转 -->
<router-link to="/home"> Home </router-link>

index.js

import Vue from 'vue'
import Router from 'vue-router'
import Home from '@/components/home'

Vue.use(Router)

export default new Router({
 mode: 'history',
  routes: [{
      path: '/home',
      name: 'Home',
      component: Home
    }
  ]
})

2、编程式导航

2.1、this.$router.push()

特点:跳转到不同的url,同时向history栈添加 一个记录(当前组件path),点击返回时返回上一页

(1)字符串

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

(2) 对象 path 属性,query属性传参数

this.$router.push({path:'/home', query:{pid:1});

特点:参数会在浏览器路径中显示,类似 get 请求

获取参数:

this.$route.query.pid

(3) 命名路由 name 属性,params属性传参数

this.$router.push({name:'home', params:{pid:1});

特点:参数会在浏览器路径中显示,类似 get 请求

参数获取:

this.$route.params.pid

2.2、this.$router.replace()

特点:作用同  push,不同的是 跳转时不会向 history栈添加 当前页 记录,无法回退

this.$router.replace({name:'home', params:{pid:1})

this.$router.replace({path:'/home', query:{pid:1})

2.3、this.$router.go(n)

特点:作用类似 window.history.go(n),从当前记录 在history栈中 向前(n > 0)查找或向后(n < 0)查找,若存在记录则跳转到该页面

this.$router.go(2)

this.$router.go(-1)

2.4、this.$router.back(n)

特点:作用回退功能,回退时判断有没有上一个路由,并跳转到该页面

this.$router.back(-1);    //返回上一页

 

 

 

 

 

 

  • 5
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值