vue页面跳转并传参的常见方式分别是什么
Admin 2022-09-13 群英技术资讯 423 次浏览
字符串、数字等。
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进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
这篇文章给大家分享的是JavaScript中字符转浮点型的方法,文中给大家介绍了两种方法,一种是使用parseFloat()方法,另一种是利用“+”运算符,对新手学习JavaScript有一定的参考价值,文中的示例介绍得很详细,有需要的朋友可以了解看看,接下来就跟随小编一起学习一下吧。
这篇文章主要为大家详细介绍了JavaScript实现二级菜单的制作,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
Jquery命名冲突解决办法很简单,只需要给jquery取一个别名即可,传统$命名形式我们都是这样使用jquery的,入下所示: scripttype=text/javascriptjQuery(function($){//thiswillsetthedivstexttoHello.$(#myDiv).text(Hello);});/scriptdivid=myDivText/div
基于element-plus的二次封装数据双向绑定在实际开发中,经常需要基于element-plus封装一些自己的定制化组件,方便快速构建我们当前的业务。在vue2.0中父子组件数据的双向绑定通
这篇文章介绍了Node.js实现压缩与解压数据的方法,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
成为群英会员,开启智能安全云计算之旅
立即注册Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008