vue路由使用,vue-router传参接参

一、通过引入vue-router.js的方式使用

1.单层路由

const chinese = { template: '<div class="main">汉语</div>' }
const english = { template: '<div class="main">英语</div>' }

const app = new Vue({

    router: new VueRouter({

        routes: [
            { path: '/chinese', component: chinese },
            { path: '/english', component: english },
            // 重定向
            {path: '/',redirect: '/chinese'}
        ]
    })
}).$mount('#app')

//访问chinese
this.$router.push('/chinese')

2.嵌套路由

contst router = new vueRouter({

    routes: [
        {path: '/', redirect: '/index'},
        {path: '/index', component: Vue.component('index')},
        {path: '/parent', name: 'parent', component: Vue.component('parent'), 
            children: [{
                // 当 /parent/:id/child 匹配成功,
                // child 会被渲染在 parent 的 <router-view> 中
                //注意: 以 / 开头的嵌套路径会被当作根路径

               path: 'child',
               component: child,
               meta: {

               }
            }]

        }
    ]

})

const app = new Vue({
    router
}).$mount('#app')

//访问child
this.$router.push('/parent/child')

二、vue-cli构建项目,路由使用(嵌套路由同上)

import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

export default new Router({
  routes: [
    {
      path: '/',
      redirect:'/index'
    },
    {
      path: '/index',
      name: 'index',
      component: () => import(/* webpackChunkName: "about" */ './views/Home.vue'),
      meta:{
        title:''
      }
    },
    {
      path: '/quizzes',
      name: 'quizzes',
      component: () => import('./views/Quizzes.vue'),
      meta:{
        title:''
      }
    },
    {
      path: '/testresult',
      name: 'testresult',
      component: () => import('./views/TestResult.vue'),
      meta:{
        title:''
      }
    }
  ]
})

 

 

三、 vue-router传参、接参

方法一:拼接在url后边
// 传递单个参数:id
this.$router.push('/studentInfo?id=' + 1);

// 获取参数id
this.$route.query; // {id:1}
//或
this.$route.query.id // 1

// 传递参数多个参数:id,name
this.$router.push('/studentInfo?id=' + 1 + '&name=' + 2);

// 获取参数
this.$route.query;// {id:1,name:2}



方法二:对象方式

// 传递参数,路径path用query传参
this.$router.push({path:'/studentInfo',query:{id:0,name:'haha'}});

// 获取参数
this.$route.query // {id:0,name:'haha'}


// 传递参数,name用params传参
this.$router.push({ name: 'studentInfo', params: { id: 0, name: 'haha' } });

// 获取参数
this.$route.params // {id:0,name:'haha'}

 

 

 

 

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值