首页 > 其他 > 详细

vue-router路由如何实现传参

时间:2019-09-15 12:04:56      阅读:106      评论:0      收藏:0      [点我收藏+]

tip: 用params传参,F5强制刷新参数会被清空,用query,由于参数适用路径传参的所以F5强制刷新也不会被清空。(传参强烈建议适用string)

也可以选用sessionstorage/localstorage/cookie存储,可以参考我的另一边文章:sessionstorage、localstorage与cookie

params:参数不会显示到路径上

1:配置路径rutes

技术分享图片
export default new Router({
  routes: [
    {
      path: ‘/testVueRouter‘,
      name: ‘TestVueRouter‘,
      component: TestVueRouter
    },
    {
      path: ‘/testVueRouterTo‘,
     // 一定要写name,params必须用name来识别路径
      name: ‘TestVueRouterTo‘,
      component: TestVueRouterTo
    }
  ]
})
技术分享图片

2:传递参数:用$router

技术分享图片
<!-- test-vue-router页面 -->
<template>
  <div>
    <a @click="routerTo()">query传参</a>
  </div>
</template>
<script>
export default {
  methods: {
    routerTo() {
      this.$router.push({
        name: `TestVueRouterTo`,
        params: {
          page: ‘1‘, code: ‘8989‘
        }
      })
    }
  }
}
</script>
技术分享图片

3:接受参数:用$route,少个r,注意啦

技术分享图片
<!-- test-vue-router-to页面 -->
<template>
  <div>
  </div>
</template>
<script>
export default{
  data() {
    return {
      page: ‘‘,
      code: ‘‘
    }
  },
  created() {
    this.getRouterData()
  },
  methods: {
    getRouterData() {
      this.page = this.$route.params.page
      this.code = this.$route.params.code
      console.log(‘page‘, this.page)
      console.log(‘code‘, this.code)
    }

  }
}
</script>
技术分享图片

技术分享图片

技术分享图片

query:最好也用name来识别,保持与params一致性,好记了,路径传参

1:路径配置(跟params一样,代码不写了)

2:传递参数页

技术分享图片
<!-- test-vue-router页面 -->
<template>
  <div>
    <a @click="routerTo()">query传参</a>
  </div>
</template>
<script>
export default {
  methods: {
    routerTo() {
      this.$router.push({
        name: `TestVueRouterTo`,
     // 只是把query改了,其他都没变 query: { page: ‘1‘, code: ‘8989‘ } }) } } } </script>
技术分享图片

3:接受参数

技术分享图片
<!-- test-vue-router-to页面 -->
<template>
  <div>
  </div>
</template>
<script>
export default{
  data() {
    return {
      page: ‘‘,
      code: ‘‘
    }
  },
  created() {
    this.getRouterData()
  },
  methods: {
    getRouterData() {
      // 只是改了query,其他都不变
      this.page = this.$route.query.page
      this.code = this.$route.query.code
      console.log(‘page‘, this.page)
      console.log(‘code‘, this.code)
    }

  }
}
</script>
技术分享图片

图片区:

技术分享图片

技术分享图片

技术分享图片

下面我们采用path: ‘/testVueRouterTo‘

1:query(成功)

技术分享图片
<!-- test-vue-router页面 -->
<template>
  <div>
    <a @click="routerTo()">query传参</a>
  </div>
</template>
<script>
export default {
  methods: {
    routerTo() {
      this.$router.push({
        path: ‘/testVueRouterTo‘,
        query: {
          page: ‘1‘, code: ‘8989‘
        }
      })
    }
  }
}
</script>
技术分享图片
技术分享图片
<!-- test-vue-router-to页面 -->
<template>
  <div>
<!--     <span>{{page}}</span>
    <span v-show="code === ‘89‘">{{code}}</span>
    <span>{{password}}</span> -->
  </div>
</template>
<script>
export default{
  data() {
    return {
      page: ‘‘,
      code: ‘‘,
      password: ‘‘
    }
  },
  created() {
    this.getRouterData()
  },
  methods: {
    getRouterData() {
      // debugger
      this.page = this.$route.query.page
      this.code = this.$route.query.code
      console.log(‘page‘, this.page)
      console.log(‘code‘, this.code)
    }

  }
}
</script>
技术分享图片

技术分享图片

2:params:(不成功)

技术分享图片
<!-- test-vue-router页面 -->
<template>
  <div>
    <a @click="routerTo()">params传参</a>
  </div>
</template>
<script>
export default {
  methods: {
    routerTo() {
      this.$router.push({
        path: ‘/testVueRouterTo‘,
        params: {
          page: ‘1‘, code: ‘8989‘
        }
      })
    }
  }
}
</script>
技术分享图片
技术分享图片
<!-- test-vue-router-to页面 -->
<template>
  <div>
<!--     <span>{{page}}</span>
    <span v-show="code === ‘89‘">{{code}}</span>
    <span>{{password}}</span> -->
  </div>
</template>
<script>
export default{
  data() {
    return {
      page: ‘‘,
      code: ‘‘,
      password: ‘‘
    }
  },
  created() {
    this.getRouterData()
  },
  methods: {
    getRouterData() {
      // debugger
      this.page = this.$route.params.page
      this.code = this.$route.params.code
      console.log(‘page‘, this.page)
      console.log(‘code‘, this.code)
    }

  }
}
</script>
技术分享图片

技术分享图片

这是由于query与params传参机制不一样,造成的差异,如果要隐藏参数用params,如果强制刷新不被清除用query

转:https://www.cnblogs.com/beka/p/8583924.html

 


http://market.szonline.net/amaz/10343.html
http://market.szonline.net/amaz/10342.html
http://market.szonline.net/amaz/10341.html
http://market.szonline.net/amaz/10340.html
http://market.szonline.net/amaz/10339.html
http://market.szonline.net/amaz/10338.html
http://market.szonline.net/amaz/10337.html
http://market.szonline.net/amaz/10336.html
http://market.szonline.net/amaz/10335.html
http://market.szonline.net/amaz/10334.html
http://market.szonline.net/amaz/10333.html
http://market.szonline.net/amaz/10332.html
http://market.szonline.net/amaz/10331.html
http://market.szonline.net/amaz/10330.html
http://market.szonline.net/amaz/10329.html
http://market.szonline.net/amaz/10328.html
http://market.szonline.net/amaz/10327.html
http://market.szonline.net/amaz/10326.html
http://market.szonline.net/amaz/10325.html
http://market.szonline.net/amaz/10324.html
http://market.szonline.net/amaz/10323.html
http://market.szonline.net/amaz/10322.html
http://market.szonline.net/amaz/10321.html
http://market.szonline.net/amaz/10320.html
http://market.szonline.net/amaz/10319.html
http://market.szonline.net/amaz/10318.html
http://market.szonline.net/amaz/10317.html
http://market.szonline.net/amaz/10316.html
http://market.szonline.net/amaz/10315.html
http://market.szonline.net/amaz/10314.html
http://market.szonline.net/amaz/10313.html
http://market.szonline.net/amaz/10312.html
http://market.szonline.net/amaz/10311.html
http://market.szonline.net/amaz/10310.html
http://market.szonline.net/amaz/10309.html
http://market.szonline.net/amaz/10308.html
http://market.szonline.net/amaz/10307.html
http://market.szonline.net/amaz/10306.html
http://market.szonline.net/amaz/10305.html
http://market.szonline.net/amaz/10304.html
http://market.szonline.net/amaz/10303.html
http://market.szonline.net/amaz/10302.html
http://market.szonline.net/amaz/10301.html
http://market.szonline.net/amaz/10300.html
http://market.szonline.net/amaz/10299.html
http://market.szonline.net/amaz/10298.html
http://market.szonline.net/amaz/10297.html
http://market.szonline.net/amaz/10296.html
http://market.szonline.net/amaz/10295.html
http://market.szonline.net/amaz/10294.html
http://market.szonline.net/amaz/10293.html
http://market.szonline.net/amaz/10292.html

vue-router路由如何实现传参

原文:https://www.cnblogs.com/zjw2004112/p/11521573.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!