vue路由传值,详细套路

1.params传值--需要用name属性,路由中必须配置name
  router.js:
import Main from './views/main.vue'
import Main2 from './views/main2.vue'
  
export default new Router({
    // mode: 'abstract',
    routes: [{
            path: '/main',
            component: Main,
        },
        {
            path: '/',
            redirect: '/main'
        }
        ,
        {	//单纯只配置path是无法使用params方式传值的,
        	//必须配置name属性才行
            name:'main2',
            path:'/main2',
            component:Main2
        }
    ]
})
  
  传值组件:
  methods: {
    jump(to) {
      //这里必须是$router
      this.$router.push({
        name:to,//这里必须使用name属性,使用path则只会跳转无法传值
        params:{
          mon:this.money
        }
      })
    }
  }

	接收组件:
  created() {
    this.money=this.$route.params.mon
    //使用params传值,这里也必须是params,同时这里是$route,不是$router
      alert(this.money)
  }
2.query传值--需要使用path属性,路由中必须配置path
  router.js:
import Main from './views/main.vue'
import Main2 from './views/main2.vue'
  
export default new Router({
    // mode: 'abstract',
    routes: [{
            path: '/main',
            component: Main,
        },
        {
            path: '/',
            redirect: '/main'
        }
        ,
        {	//单纯只配置path只能使用query方式传值
            //name:'main2',
            path:'/main2',
            component:Main2
        }
    ]
})
  
  
传值组件:
  methods: {
    jump(to) {
      //这里必须是$router
      this.$router.push({
        path:to,//这里必须使用path属性
        query:{
          mon:this.money
        }
      })
    }
  }

接收组件:
  created() {
    this.money=this.$route.query.mon//使用query传值,这里也必须是query,同时这里是$route,不是$router
      alert(this.money)
  }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值