1.声明式导航router-link
// 注意:router-link中链接如果是'/'开始就是从根路由开始,如果开始不带'/',则从当前路由开始。
<router-link :to="{name:'home'}">
<router-link :to="{path:'/home'}"> //name,path都行, 建议用name
1.2
<router-link :to="{name:'home', params: {id:1}}">
<router-link :to="{name:'home', query: {id:1}}">
<router-link :to="/home/:id">
//传递对象
<router-link :to="{name:'detail', query: {item:JSON.stringify(obj)}}"></router-link>
2.编程式导航 this.$router.push()
不带参数
this.$router.push('/home')
this.$router.push({name:'home'})
this.$router.push({path:'/home'}
带参数 query传参
1.路由配置:
name: 'home',
path: '/home'
2.跳转:
this.$router.push({name:'home',query: {id:'1'}})
this.$router.push({path:'/home',query: {id:'1'}})
3.获取参数
html取参: $route.query.id
script取参: this.$route.query.id
3.params传参
1.路由配置:
name: 'home',
path: '/home/:id'(或者path: '/home:id')
2.跳转:
this.$router.push({name:'home',params: {id:'1'}})
注意:
// 只能用 name匹配路由不能用path
// params传参数(类似post) 路由配置 path: "/home/:id" 或者 path: "/home:id"否则刷新参数消失
3.获取参数
html取参:$route.params.id
script取参:this.$route.params.id
4.直接通过path传参
1.路由配置:
name: 'home',
path: '/home/:id'
2.跳转:
this.$router.push({path:'/home/123'})
或者:
this.$router.push('/home/123')
3.获取参数:
this.$route.params.id
5.this.$router.go(n)
向前或者向后跳转n个页面,n可为正整数或负整数
6.跳转页面打开新窗口并携带参数
const routeData = this.$router.resolve({
path: `/workbench/customer_detail/${this.audioFrom.import_id}`
})
window.open(routeData.href, '_blank')
7.跳转新项目并携带参数
window.open(`https://hao123/#/workbench/customer_detail/${this.audioFrom.import_id}`)
|