一、router-link跳转(最简单的跳转方式)
// 需要在router目录下的index.js里先指定跳转页面
<roter-link to="/login">跳转</roter-link>
二、this.$router.push()跳转
此跳转方式常用于传参并跳转,会被记录到History栈中,可以对页面进行返回操作!
// 都需要先在router目录下的index.js里先指定跳转页面
1. 不带参数
this.$router.push('/home')
this.$router.push({name:'home'})
this.$router.push({path:'/home'})
2. query传参(会把参数追加到请求中)
this.$router.push({name:'home',query: {id:'1'}})
this.$router.push({path:'/home',query: {id:'1'}})
// html 取参 $route.query.id
// script 取参 this.$route.query.id
3. params传参(不会把参数追加到请求中)
this.$router.push({name:'home',params: {id:'1'}}) // 只能用 name
// params传参时push里必须是name,不能使用path。params 只能用 name 来引入路由,如果这里写成了 path ,接收参数页面会是 undefined。
// 路由配置 path: "/home/:id" 或者 path: "/home:id" ,
// 不配置path ,第一次可请求,刷新页面id会消失
// 配置path,刷新页面id会保留
// html 取参 $route.params.id
// script 取参 this.$route.params.id
4. query和params区别
query类似 get, 跳转之后页面 url后面会拼接参数,类似?id=1, 非重要性的可以这样传, 密码之类还是用params刷新页面id还在
params类似 post, 跳转之后页面 url后面不会拼接参数 , 但是刷新页面id 会消失
三、this.$router.replace()
此跳转方式常用于直接跳转,不会被记录到History栈中,无法对页面进行返回操作!
用法同上