zoukankan      html  css  js  c++  java
  • vue 路由跳转及传值和取值

     vue 路由跳转及传值和取值

    一.声明式:

    使用router-link标签中的to属性:

    1.无参数: 

    <router-link :to="{name:'index'}}">    或者    <router-link to='/index'>

     2.带参数:

    <router-link :to="{name:'index',query:{id:'xxx',name:'xxx'}}">

    二.编程式:

    替换:

        this.$router.replace{path:‘/login’ } //一般用户跳转错误页或登录页,这种路由时使用

    push方法: 

    1.无参数:

    this.$router.push('路由地址')
    this.$router.go(-1)  // 后退1步
    this.$router.push({path:'路径')};//使用路由地址
    this.$router.push({name:'路由')};//使用路由名称

    2.带参数:

    使用query传参及取值:

    //传参:
    this
    .$router.push({path:'/test',query:{name:'张三', age:18}}); //url方式传参,等价于<a href="http://baidu.com?aa=xx&bb=xx">百度</a>

    //在接收页面取值
    this.$route.query.name//获取name值,其他雷同

    使用params传参:

    //传参
    this
    .$router.push({name:'测试页',params:{name:'张三', age:18}});

    //取值
    this.$route.params.name
    //坑一:name代表参数名称,获取不到参数时,请看这句话路由的params对象使用,必须要通过路由名来调用路由,而不同通过path来调用,而query对象则没有这个要求。
    //坑二:如果有子路由,并且父路由使用了redirect属性重定向到子路由时,传参时name一定是子路由的name(深坑)
    路由配置如下:
    {
      name:'测试',//非此name
      path: '/test',
      component: Layout,
      redirect: '/test/index',//使用此属性
      children: [{
        path: 'index',
        name: '测试页',//子路由名称
        meta: {
          i18n: 'test'
        },
        component: () =>
          import( /* webpackChunkName: "views" */ '@/views/util/test')
      }]
    }, 
     
     

    使用路由地址传参:(动态路由)

    //传参
    this
    .$router.push({path:'/test${id}'});//此方式传参,需要调整路由

    //取值
    this.$route.params.id//id代表参数名称
    //路由配置如下:
    {
      path: '/test:id',//变化点
      component: Layout,
      redirect: '/test/index',
      children: [{
        path: 'index',
        name: '测试',
        meta: {
          i18n: '测试页'
        },
        component: () =>
          import( /* webpackChunkName: "views" */ '@/views/test/index')
      }]
    }
  • 相关阅读:
    滚动条滑至底部自动加载内容
    curl请求https请求
    JS根据经纬度获取地址信息
    php结合md5的加密解密算法实例
    php gzcompress() 和gzuncompress()函数实现字符串压缩
    html视频播放器的代码 及其参数详解
    phpcms 整合 discuz!
    phpcms V9 整合 Discuz! X2 教程
    中国各省打架排行榜
    jQuery获取输入框并设置焦点
  • 原文地址:https://www.cnblogs.com/zlp520/p/14090687.html
Copyright © 2011-2022 走看看