vue路由跳转的带参与不带参,路由跳转传参方式:name 、 path
前言:在vue项目中路由的router跳转是很重要的,这里有我自用跳转的方法,最底下还有大神的方法(很详细了);自己目前用到的方法一:用path跳转加参数,相当于get请求,页面可以看到你传参第一:设置路由{path: 'Train/studyDetails/:detailId', component: () => import('./views/team...
·
前言:
在vue项目中路由的router跳转是很重要的,这里有我自用跳转的方法,最底下还有大神的方法(很详细了);
自己目前用到的方法一:用path跳转加参数,相当于get请求,页面可以看到你传参
第一:设置路由
{path: 'Train/studyDetails/:detailId', component: () => import('./views/team/Train/studyDetails'), meta: { title: '培训学习详情' }},
第二:父组件传值
routerDeatils(data){//点击表格跳路由
let nowId = '7';
this.$router.push({path:'/team/Train/studyDetails/'+nowId});
},
第三:在子组件里面接收这个参数
created(){
this.detailId = this.$route.params.detailId;
},
自己目前用到的方法二:用name跳转,params传参,相当于post请求,页面不可以看到你传参值
第一:在跳转页面用name来定义跳转的路径,用params来定义需要携带的参数
/**
* 跳转到详情页面
*/
sendDetails(id){
this.$router.push({ name:'readDetails', params:{ id : id } });//用name跳转的方式可以实现带参数,而且不在页面显示,注意用path跳转的话,params是不生效的
}
第二:在调用页面获取id
let id = this.$route.params.id;
------------------------------------------------------------------------------------------------------------------------------------
更多方法:
1. router-link
1. 不带参数
<router-link :to="{name:'home'}">
<router-link :to="{path:'/home'}"> //name,path都行, 建议用name
// 注意:router-link中链接如果是'/'开始就是从根路由开始,如果开始不带'/',则从当前路由开始。
2.带参数
<router-link :to="{name:'home', params: {id:1}}">
// params传参数 (类似post)
// 路由配置 path: "/home/:id" 或者 path: "/home:id"
// 不配置path ,第一次可请求,刷新页面id会消失
// 配置path,刷新页面id会保留
// html 取参 $route.params.id
// script 取参 this.$route.params.id
<router-link :to="{name:'home', query: {id:1}}">
// query传参数 (类似get,url后面会显示参数)
// 路由可不配置
// html 取参 $route.query.id
// script 取参 this.$route.query.id
2. this.$router.push() (函数里面调用)
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
// 路由配置 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 会消失
3. this.$router.replace() (用法同上,push)
4. 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可为正整数或负整数
参考文献:
1.https://blog.csdn.net/jiandan1127/article/details/86170336
2.https://blog.csdn.net/weixin_30443895/article/details/95238305
更多推荐
已为社区贡献102条内容
所有评论(0)