vue页面跳转并传参的常见方式分别是什么
Admin 2022-09-13 群英技术资讯 612 次浏览
字符串、数字等。
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浅拷贝和深拷贝,javascript中的对象是引用类型,在复制对象的时候就要考虑是用浅拷贝还是用深拷贝。接下来一起聊聊吧,感兴趣的小伙伴也可以参考一下
前端开发人员只要了解过vue.js框架可能都知道单文件组件。vue.js中的单文件组件允许在一个文件中定义一个组件的所有内容。这是一个非常有用的解决方案,在浏览器网页中已经开始提倡这种机制。
我们商品分类的信息太多,如果每一行都显示同一个颜色的话会让人看的眼花,为了提高用户体验,减少用户看错的情况,需要对表格进行隔行
nodejs中的异步迭代器是什么?一些朋友对于异步迭代器比较好奇,因此这篇文章就和大家来分享一下异步迭代器以及其作用,下文有详细的介绍及实例供大家参考,感兴趣的朋友就继续往下看吧。
这篇文章主要介绍了如何在React中直接使用Redux,目前redux在react中使用是最多的,所以我们需要将之前编写的redux代码,融入到react当中去,本文给大家详细讲解,需要的朋友可以参考下
成为群英会员,开启智能安全云计算之旅
立即注册Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008