美文网首页
路由传参总结

路由传参总结

作者: 稻草人_9ac7 | 来源:发表于2019-12-04 21:47 被阅读0次

第一种方式:动态路由参数

//当前页面的配置
<template>
       <div>
         //第一种跳转和传参的方式
      <router-link to="/detail/aaa/12">跳转吧</router-link>
        //第二种跳转传参的方式,使用事件
<button @click="goDetail">跳转吧</button>
         <Navgation></Navgation>
    </div>
</template>
<script>

import Navgation from '@/components/Navigation'
export default {      
    data(){
        return{          

        }
    },
    methods: {       
      goDetail(){
      this.$router.push('/detail/老王/23')
       }
      
    },
    computed: {
      
    },
    components:{
 
        Navgation
   
    }
    
}
</script>
<style lang="less" scoped>
 
</style>
//要跳转到的页面
<template>
    <div>
        <div>这是详情页名字:{{name}}</div>
        <div>这是详情页年龄:{{age}}</div>
    </div>
</template>
<script>
export default {
    data() {
        return {
            msg:""
        }
    },
    created() {
        this.name=this.$route.params.name
        this.age=this.$route.params.age

        console.log("msg",this.$route.params)
        
    },
}
</script>
//路由的配置
    {
        path: "/detail/:name/:age",
        name: "detail",
        component: () =>
            import ('@/components/Detail')
    }

特点是:
1.刷新后,穿过来的参数不会消失
2.传递的参数信息会拼接在URL后面
3.不管通过<router-link to="/detail/aaa/12">跳转吧</router-link>还是this.$router.push('/detail/老王/23'),都可以实现相同的效果
效果:

效果图

第二种路由传参方式:通过name来匹配路由,通过param来传递参数

//当前页面
<template>
       <div>        
      
        <button @click="goDetail">跳转吧</button>
         <Navgation></Navgation>
    </div>
</template>
<script>

import Navgation from '@/components/Navigation'
export default {      
    data(){
        return{
           

        }
    },
    methods: {       
       goDetail(){  
      this.$router.push({
          name:'detail',
          params:{
              name:"小李子",
              age:12 
          }
      })
       }      
    },
    computed: {
      
    },
    components:{
 
        Navgation
   
    }
    
}
</script>
<style lang="less" scoped>
 
</style>
//要跳转到的页面
<template>
    <div>
        <div>这是详情页名字:{{name}}</div>
        <div>这是详情页年龄:{{age}}</div>
    </div>
</template>
<script>
export default {
    data() {
        return {
            msg:""
        }
    },
    created() {
        this.name=this.$route.params.name
        this.age=this.$route.params.age

        console.log("msg",this.$route.params)
        
    },
}
</script>
//路由的配置
   {      
        path: "/detail",
        name: "detail",
        component: () =>
            import ('@/components/Detail')
    }

分析:刷新后,传过来的数据就没了
不拼接在URL地址栏上

当我们
效果图


效果图

第三种路由传参方式:path+query;query传递的参数会通过?id = xxx展示

//当前页面
<template>
       <div>        
              <button @click="goDetail">跳转吧</button>
         <Navgation></Navgation>
    </div>
</template>
<script>

import Navgation from '@/components/Navigation'
export default {      
    data(){
        return{           

        }
    },
    methods: {       
       goDetail(){  
      this.$router.push({
          path:'/detail',
          query:{
              name:"刘能",
              age:12 
          }
      })
       }      
    },
    computed: {
      
    },
    components:{ 
        Navgation   
    }    
}
</script>
<style lang="less" scoped>
 
</style>
//要跳转到的页面
<template>
    <div>
        <div>这是详情页名字:{{name}}</div>
        <div>这是详情页年龄:{{age}}</div>
    </div>
</template>
<script>
export default {
    data() {
        return {
            msg:""
        }
    },
    created() {
        this.name=this.$route.query.name
        this.age=this.$route.query.age
        console.log("msg",this.$route.query)        
    },
}
</script>
//路由的pe
    {

        path: "/detail",

        name: "detail",
        component: () =>
            import ('@/components/Detail')
    }

效果图:


效果图

分析:
1.传的参数拼接在URL后面
2.刷新后传过来的参数不会消失

相关文章

  • Vue实战第二天

    路由组件传参 动态路由传参 静态路由传参 函数传参htm5 history 模式 设置通用路由,找不到页面跳转自定...

  • vue路由 filters方法和filter过滤用法总结

    限制单行文字长度 路由传参写法 ---- 声明式 路由传参写法 ---- 编程式 路由传参写法 ---- 路由里要...

  • Next.js 跳转传参并接收接参

    介绍路由传参,接参使用方法 传参 + 跳转页面接收参数 动态路由传参 + 跳转页面接收参数创建动态路由在pag...

  • 路由传参总结

    第一种方式:动态路由参数 特点是:1.刷新后,穿过来的参数不会消失2.传递的参数信息会拼接在URL后面3.不管通过...

  • vue-router

    路由安装 路由配置 路由跳转 路由传参-param使用params传参只能使用name进行引入http://loc...

  • Vue 动态路由

    动态路由 动态路由传参

  • vue中组件3种编程式路由跳转传参

    路由传参 1、路由配置传参(刷新页面不会丢失参数,url会携带参数) A组件跳转B组件传参A组件 路由配置 B组件...

  • vue路由传参.md

    两种传参方法 1.命名路由传参(name, params方式) 2.路由路径传参(path, query方式...

  • nuxt路由及传参

    最近的nuxt项目中经常会用到路由传参,故在此有所总结了下nuxt中传参的方式和vue项目中传参的方式 nuxt ...

  • 路由传参

    路由传参

网友评论

      本文标题:路由传参总结

      本文链接:https://www.haomeiwen.com/subject/ixylgctx.html