vue-router路由跳转&路由传参

淡淡的烟草味﹌ 2023-10-06 17:18 70阅读 0赞

对于大多数单页面应用,都推荐使用官方支持的 vue-router 库。更多细节可以移步 vue-router文档 。

  1. <router-link :to="需要跳转到的页面的路径">
  2. <router-link :to="home">//路由链接
  3. <!--或<router-link :to="{name:'home'}">-->
  4. <!--或<router-link :to="{path:'/home'}"> -->//name,path都行, 建议用name
  5. <router-view></router-view>//用于渲染当前路由组件
  6. 使用replace不会有浏览历史,所以不能点击返回上一个页面
  7. <router-link to="/shop/goods" replace>点餐</router-link>

跳转方式二:编程式导航

$route:表示当前页面

  1. this.$router.push(path) //: 相当于点击路由链接(可以返回到当前路由界面)

    this.$router.push(‘/home’)
    this.$router.push({name:’home’})
    this.$router.push({path:’/home’})

  2. this.$router.replace(path) //: 用新路由替换当前路由(不可以返回到当前路由界面)

  3. this.$router.back() //: 请求(返回)上一个记录路由
  4. this.$router.go(-1) //:请求(返回)上一个记录路由
  5. this.$router.go(1) //: 请求下一个记录路由

    返回

路由传参

方式 1: 路由路径携带参数(param/query)

  1. 1) 配置路由
  2. children: [
  3. {
  4. path: 'mdetail/:id', component: MessageDetail
  5. }
  6. ]
  7. 2) 路由路径
  8. <router-link :to="'/home/message/mdetail/'+m.id">{
  9. {m.title}}</router-link>
  10. 3) 路由组件中读取请求参数
  11. this.$route.params.id

方式 2: 属性携带数据

  1. <router-view :msg="msg"></router-view>
  2. <template>
  3. <div>
  4. <h2>About</h2>
  5. <p>{
  6. {msg}}</p>
  7. <input type="text">
  8. </div>
  9. </template>
  10. <script>
  11. export default {
  12. props: {
  13. msg: String
  14. }
  15. }
  16. </script>

变成式路由导航传参

使用query传参

  1. 1.路由配置:
  2. name: 'home',
  3. path: '/home'
  4. 2.跳转:
  5. this.$router.push(
  6. {
  7. name:'home',
  8. query: {id:'1'}
  9. }
  10. )
  11. this.$router.push(
  12. {
  13. path:'/home',
  14. query: {id:'1'}}
  15. )
  16. 3.获取参数
  17. html取参: $route.query.id
  18. script取参: this.$route.query.id

使用params传参

  1. 1.路由配置:
  2. name: 'home',
  3. path: '/home/:id'(或者path: '/home:id')
  4. 2.跳转:
  5. this.$router.push(
  6. {
  7. name:'home',
  8. params: {id:'1'}
  9. }
  10. )
  11. 注意:
  12. // 只能用 name匹配路由不能用path
  13. // params传参数(类似post) 路由配置 path: "/home/:id" 或者 path: "/home:id"否则刷新参数消失
  14. 3.获取参数
  15. html取参:$route.params.id
  16. script取参:this.$route.params.id

直接通过path传参

  1. 1.路由配置:
  2. name: 'home',
  3. path: '/home/:id'
  4. 2.跳转:
  5. this.$router.push({path:'/home/123'})
  6. 或者:
  7. this.$router.push('/home/123')
  8. 3.获取参数:
  9. this.$route.params.id

params和query的区别

query类似 get,跳转之后页面 url后面会拼接参数,类似?id=1。
非重要性的可以这样传,密码之类还是用params,刷新页面id还在。
params类似 post,跳转之后页面 url后面不会拼接参数。

完整例子

在putId.vue文件中

  1. <template>
  2. <li @click="change">编程式路由传参</li>
  3. </template>
  4. <script>
  5. export default {
  6. data () {
  7. return {
  8. id:2, //需要传递的参数
  9. }
  10. },
  11. methods:{
  12. change(){
  13. this.$router.push({
  14. //核心语句
  15. path:'/getId', //跳转的路径
  16. query:{
  17. //路由传参时push和query搭配使用 ,作用时传递参数
  18. id:this.id ,
  19. }
  20. })
  21. }
  22. }
  23. }
  24. </script>

在getId.vue文件中

  1. <template>
  2. <div>
  3. {
  4. {id}}
  5. </div>
  6. </template>
  7. <script>
  8. export default{
  9. data(){
  10. return{
  11. id:'',
  12. }
  13. },
  14. created(){
  15. this.id = this.$route.query.id,
  16. console.log(this.id)
  17. }
  18. }
  19. </script>

this.$router.replace{path:‘/’ }类似

发表评论

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

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

相关阅读

    相关 VueRouter

    1. 前言 本小节我们介绍 VueRouter 路由组件传参。包括 params 传参、query 传参的两种方式。路由传参的知识点非常重要,在日常开发中,我们经常会通过

    相关 Vue+

    之前在原生JS的开发中,我们经常会用到根据某一状态进行页面的跳转。 比如:登录成功跳到首页,点击商品列表的某个商品跳转商品详情等。 而常见的写法就是: locat