vue页面跳转并传参的常见方式分别是什么
Admin 2022-09-13 群英技术资讯 1374 次浏览
这篇文章主要介绍了“vue页面跳转并传参的常见方式分别是什么”相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇vue页面跳转并传参的常见方式分别是什么文章都会有所收获,下面我们一起来看看吧。字符串、数字等。
path:要跳转新页面的路由链接
query:要携带的参数
let pathInfo = this.$router.resolve({
path:'/product_detail',
query:{
productId:'11'
}
})
window.open(pathInfo.href, '_blank');
新页面的参数接收:
this.productId = this.$route.query.productId
对象、数组等,通过JSON转换进行传递。
let pathInfo = this.$router.resolve({
path:'/product_detail',
query:{
data:{name:'张三'}
}
})
window.open(pathInfo.href, '_blank');
新页面的参数接收:
console.log(this.$route.query.data)
我们知道,在vue中每个页面都需要在路由中声明,就是在router/index.js中写下面代码:
import Vue from 'vue'
import Router from 'vue-router'
import Test from "../components/Test";
Vue.use(Router)
export default new Router({
mode: 'history',
routes: [
{
path: '/t',
name: 'Test',
component: Test,
hidden:true
},
]
})
实现页面跳转并传参有多种方式:
在template中可以使用<router-link>标签实现跳转,跳转的路径是http://localhost:8080/t?index=id,如下:
<router-link to="/t?index=1">
<button class="btn btn-default">点击跳转</button>
</router-link>
只需要点击按钮就可以实现跳转,不需要写js代码,需要传递参数的话只需要/t?index=1即可,这样的话跳转的页面获取参数通过window.location.href能够获取到完整的url,然后截取参数。也可以通过下面代码获取参数
this.$route.query.index
跳转的路径是http://localhost:8080/t?index=id
<router-link :to="{path:'/t',query: {index: 1}}">
<button class="btn btn-default">点击跳转</button>
</router-link>
其中需要注意,这里的to前面一定要加冒号,path的值要和上面路由定义的值一致,传参用query,里面是参数字典。
接收参数:
this.$route.query.index
命名路由的方式:
跳转的路径是http://localhost:8080/t?index=id
<router-link :to="{name:'Test',params: {index: 1}}">
<button class="btn btn-default">点击跳转</button>
</router-link>
注意这里的name也要和router/index.js中声明的name值一致,并且传参使用params,和name配对的是params,和path配对的是query。
接收参数:
this.$route.params.index
跳转的路径是http://localhost:8080/t/id
<router-link:to="'/test/'+1">
<button class="btn btn-default">点击跳转</button>
</router-link>
这时的路由也需要更为为下面的形式:
routes: [
{
path: '/t/:index',
name: 'Test',
component: Test,
hidden:true
},
]
接收参数:
this.$route.params.index
上面四种方法都是在html中实现的跳转,还有另外对应的在js中实现的跳转并传参的方法,代码如下:
<template>
<button @click = "func()">跳转</button>
</template>
<script>
export default{
methods:{
func (){
this.$router.push({path: '/t?index=1'});
}
}
}
</script>
接收参数依然使用
this.$route.query.index
<template>
<button @click = "func()">跳转</button>
</template>
<script>
export default{
methods:{
func (){
this.$router.push({path: '/t',query:{ index:'1'}});
}
}
}
</script>
接收参数依然使用
this.$route.query.index
<template>
<button @click = "func()">跳转</button>
</template>
<script>
export default{
methods:{
func (){
this.$router.push({path: '/t/index'});
}
}
}
</script>
接收参数依然使用
this.$route.query.index
<template>
<button @click = "func()">跳转</button>
</template>
<script>
export default{
methods:{
func (){
this.$router.push({name: 'Test',params:{ index:'1'}});
}
}
}
</script>
接收参数依然使用
this.$route.params.index
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
不少朋友应该都有玩过贪吃蛇小游戏吧,贪吃蛇小游戏可谓是经典游戏了,那么我们如果使用原生JS怎么写一个贪吃蛇小游戏呢?下面就给大家分享使用原生js实现贪吃蛇游戏代码,感兴趣的朋友可以参考学习。
这篇文章主要为大家详细介绍了vue+elementUI实现分页效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
javascript是单线程。作为浏览器脚本语言,JavaScript的主要用途是与用户互动,以及操作DOM;这决定了它只能是单线程,否则会带来很复杂的同步问题。
今天给大家分享的是用JS实现一个简易的消息提示框的内容,小编觉得比较实用,对新手学习JavaScript有一定的参考价值,实现效果及代码如下,感兴趣的朋友就继续往下看吧。
axios怎样实现请求封装及使用?通常,如果我们是做大型的项目,那么就需要使用到很多接口,而为了使用的方便,将接口封装起来很一个不错的方法,因此下面小编就能给大家分享axios的简单封装,需要的朋友可以参考。
成为群英会员,开启智能安全云计算之旅
立即注册关注或联系群英网络
7x24小时售前:400-678-4567
7x24小时售后:0668-2555666
24小时QQ客服
群英微信公众号
CNNIC域名投诉举报处理平台
服务电话:010-58813000
服务邮箱:service@cnnic.cn
投诉与建议:0668-2555555
Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 ICP核准(ICP备案)粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008