当前位置: 移动技术网 > IT编程>脚本编程>vue.js > vue params、query传参使用详解

vue params、query传参使用详解

2017年12月12日  | 移动技术网IT编程  | 我要评论

网站反应速度测试,钱佳,同创新佳

最近在学习vue,本文介绍了vue params、query传参使用,分享给大家,也给自己留个笔记

声明式:<router-link :to="...">

编程式:router.push(...)

这两种方式 都可以实现跳转链接,在上篇文章继续,通过a组件跳转链接到b组件并且传参数。

1、router.push使用

router/index.js

export default new router({
 routes: [
   {
   path: '/',
   name: 'a',
   component: require('../components/a')
  },
  {
   path: '/b/:name/:age',
   name: 'b',
   component: require('../components/b')
  }
 ]
})

上边,在路由中为b组件添加两个参数 name ,age

a组件,绑定一个@click事件,跳转b组件传参 使用params

<template>
 <div> <!---只允许有一个最外层标签 !-->
  <div>
   <p>{{message}}</p>
   <p @click="tobfun">跳转b组件啊啊</p>
   <!--<router-link :to="{ path: '/b',params:{name:'zs',age:22}}">跳转b组件啊啊</router-link>-->
  </div>
 </div>
</template>
<script>
 export default {
  data: function () {
   return {
    message: 'vue好帅啊!'
   }
  },
  methods: {
   tobfun: function(){
    this.$router.push({name:'b',params:{name:'xy',age:22}});
   }
  }
 }
</script>
<style>

</style>

这时浏览器会显示 :http://localhost:8080/#/b/xy/22

在看下query  传值及地址变化

同样在 router/index.js路由文件中 不变有两个参数name,age

 {
   path: '/b/:name/:age',
   name: 'b',
   component: require('../components/b')
  }

在a组件中,之前参数传递是通过params,

this.$router.push({name:'b',params:{name:'xy',age:22}});

替换后,query

 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

 <router-link :to="{ path: '/b',query:{name:'张飞',age:22}}">跳转b组件</router-link>

跳转后,浏览器地址为:http://localhost:8080/#/b?name=zzz&age=22

跟  this.$router.push(...) 是一样的

 <router-link :to="{path:'/b/123'}">
    跳转b组件</router-link>
  </div>
{
   path: '/b/:name',
   name: 'b',
   component: require('../components/b')
  }

取值

this.$route.params.name

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网