vue之this.$route.query和this.$route.params的使用与区别

Bertha 。 2023-06-20 13:59 62阅读 0赞
  1. 一、this.$route.query的使用
  2. 1router/index.js
  3. {
  4. path:'/mtindex',
  5. component: mtindex, //添加路由
  6. children:[
  7. {
  8. path:':shopid',
  9. component:guessdetail
  10. }
  11. ]
  12. },
  13. 2、传参数
  14. this.$router.push({
  15. path: '/mtindex/detail', query:{shopid: item.id}
  16. });
  17. 3、获取参数
  18. this.$route.query.shopid
  19. 4url的表现形式(url中带有参数)
  20. http://localhost:8080/#/mtindex/detail?shopid=1
  21. 二、this.$route.params
  22. 1router/index.js
  23. {
  24. path:'/mtindex',
  25. component: mtindex, //添加路由
  26. children:[
  27. {
  28. path:"/detail",
  29. name:'detail',
  30. component:guessdetail
  31. }
  32. ]
  33. },
  34. 2、传参数( params相对应的是name query相对应的是path
  35. this.$router.push({
  36. name: 'detail', params:{shopid: item.id}
  37. });
  38. 3、获取参数
  39. this.$route.params.shopid
  40. 4url的表现形式(url中没带参数)
  41. http://localhost:8080/#/mtindex

例子:

  1. routes: [
  2. // 写法 (1)
  3. { //在路由中显示传递的参数
  4. path: '/skipIn/:name/:age', //传递两个参数
  5. name: 'SkipIn', //跳转页面的名字
  6. component: SkipIn //注册组件
  7. },
  8. // 写法 (2)
  9. // {
  10. // path: '/skipIn',
  11. // name: 'SkipIn',
  12. // component: SkipIn
  13. // }
  14. ]

跳转之前的页面

  1. <template>
  2. <div id="skip">
  3. <div class="Input">
  4. <el-form ref="form" :model="form" label-width="80px">
  5. <el-row :gutter="20">
  6. <el-col :span="6">
  7. <el-form-item label="姓名:">
  8. <el-input v-model="form.name" size="small"></el-input>
  9. </el-form-item>
  10. </el-col>
  11. <el-col :span="6">
  12. <el-form-item label="年龄:">
  13. <el-input v-model="form.age" size="small"></el-input>
  14. </el-form-item>
  15. </el-col>
  16. </el-row>
  17. </el-form>
  18. </div>
  19. <div class="footer">
  20. <el-button type="primary" size="small" class="skipBtn" @click="skipBtn">路由跳转</el-button>
  21. </div>
  22. </div>
  23. </template>
  24. <script>
  25. export default{
  26. name:'RouterSkip',
  27. data(){
  28. return{
  29. form:{
  30. name: "",
  31. age: ""
  32. }
  33. }
  34. },
  35. methods:{
  36. skipBtn: function(){
  37. // 写法 1.如果以这种方式传递参数,那么路由的写法要以(1)为准。
  38. // url的表现形式为(url中带参数):http://localhost:8080/#/skipIn/小明/20
  39. this.$router.push({ path: "/skipIn/" + this.form.name + "/" + this.form.age});
  40. // 写法 2. 如果以这种方式传递参数,那么路由的写法要以(2)就可以了。
  41. // url的表现形式为(url中不带参数):http://localhost:8080/#/skipIn
  42. this.$router.push({ name: 'SkipIn', params:{name: this.form.name, age:this.form.age}});
  43. // 注:如果以2这种方式传递参数时,那么当刷新跳转后传参的页面,数据将不存在。
  44. // 写法 3. 如果以这种方式传递参数,那么路由的写法要以(2)就可以了。
  45. // url的表现形式为(url中带参数):http://localhost:8080/#/skipIn?name=小明&age=20
  46. this.$router.push({ path: "/skipIn", query: {name: this.form.name, age:this.form.age}});
  47. // 注:如果以1、3这种方式传递参数时,刷新跳转后传参的页面,数据还会显示存在。
  48. }
  49. },
  50. }
  51. </script>
  52. <style lang="scss" scoped>
  53. #skip{
  54. width: 100%;
  55. height: 400px;
  56. background: #eee;
  57. .Input{
  58. padding: 10px 20px;
  59. }
  60. .footer{
  61. width: 100%;
  62. background: #ccc;
  63. padding: 5px 20px;
  64. overflow: hidden;
  65. .skipBtn{
  66. float: right;
  67. }
  68. }
  69. }
  70. </style>

跳转之后的页面

  1. <template>
  2. <div id="skipIn">
  3. <div class="header">
  4. <span class="info">{
  5. {msg}}</span>
  6. <el-button type="primary" size="small" class="backBtn" @click="back">
  7. 返回<i class="iconfont icon-fanhui back"></i>
  8. </el-button>
  9. </div>
  10. <div class="information">{
  11. {params}}</div>
  12. </div>
  13. </template>
  14. <script>
  15. export default{
  16. name:'SkipIn',
  17. data(){
  18. return{
  19. msg: "路由传参页面",
  20. params: ""
  21. }
  22. },
  23. methods:{
  24. back: function(){
  25. this.$router.go(-1); //返回
  26. },
  27. showInfo: function(){
  28. // 对应写法 1. 2. 获取传过来的参数
  29. this.params = this.$route.params.name;
  30. // 对应写法 3. 获取传过来的参数
  31. this.params = this.$route.query.name;
  32. }
  33. },
  34. mounted(){
  35. this.showInfo();
  36. }
  37. }
  38. </script>
  39. <style lang="scss" scoped>
  40. #skipIn{
  41. width: 100%;
  42. height: 400px;
  43. background: red;
  44. .header{
  45. width: 100%;
  46. background: #eee;
  47. padding: 5px 20px;
  48. overflow: hidden;
  49. .info{
  50. font-size: 14px;
  51. line-height: 32px;
  52. }
  53. .backBtn{
  54. float: right;
  55. .back{
  56. font-size: 14px;
  57. }
  58. }
  59. }
  60. .information{
  61. font-size: 20px;
  62. }
  63. }
  64. </style>

发表评论

表情:
评论列表 (有 0 条评论,62人围观)

还没有评论,来说两句吧...

相关阅读

    相关 vuereact区别我见

    react和vue都是做组件化的,整体的功能都类似,但是他们的设计思路是有很多不同的。使用react和vue,主要是理解他们的设计思路的不同。 1.数据是不是可变的 r