Vue.js params、query传参使用详解
发布于 2020-2-24|
复制链接
本篇文章主要介绍了vue params、query传参使用详解,小妖觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小妖过来看看吧
最近在学习Vue,本文介绍了vue params、query传参使用,分享给大家,也给自己留个笔记声明式:编程式:router.push(...)这两种方式 都可以实现跳转链接,在上篇文章继续,通过A组件跳转链接到B组件并且传参数。1、router.push使用router/index.js
```javascript
export default new Router({
routes: [
{
path: '/',
name: 'A',
component: require('../components/A')
},
{
path: '/B/:name/:age',
name: 'B',
component: require('../components/B')
}
]
})
```
上边,在路由中为B组件添加两个参数 name ,ageA组件,绑定一个@click事件,跳转B组件传参 使用params
```xhtml
{{message}}
跳转B组件啊啊
跳转B组件啊啊-->
export default {
data: function () {
return {
message: 'vue好帅啊!'
}
},
methods: {
toBFun: function(){
this.$router.push({name:'B',params:{name:'xy',age:22}});
}
}
}
```
这时浏览器会显示 :http://localhost:8080/#/B/xy/22在看下query 传值及地址变化同样在 router/index.js路由文件中 不变有两个参数name,age
```javascript
{
path: '/B/:name/:age',
name: 'B',
component: require('../components/B')
}
```
在A组件中,之前参数传递是通过params,
```javascript
this.$router.push({name:'B',params:{name:'xy',age:22}});
```
替换后,query
```javascript
this.$router.push({name:'B',query:{name:'xy',age:22}});
```
这时浏览器会发现:http://localhost:8080/#/?name=xy&age=22 通过以上两种,页面刷新后,参数还会保留的。获取值有些不相同:
params:this.$route.params.name;
query:this.$route.query.name;------------------------ 还有种方式-------------------------------------------- 使用 router-link
```xhtml
跳转B组件
```
跳转后,浏览器地址为:http://localhost:8080/#/B?name=zzz&age=22跟 this.$router.push(...) 是一样的
```xhtml
跳转B组件
```
```javascript
{
path: '/B/:name',
name: 'B',
component: require('../components/B')
}
```
取值
```javascript
this.$route.params.name
```