Vue中路由的配置

vue中路由的使用

 

1.安装:npm install vue-router --save(写入package.json)

2.引入:在router文件夹下的index.js中import Router from 'vue-router'//12在以cli2创建的项目中已经完成

3.引入组件:在router文件夹下的index.js

  import HelloWorld from '@/components/HelloWorld'  
  import Home from '@/components/Home'

4.配置路由:在router文件夹下的index.js
 

   const routes = [
     {
        path: '/home/:aid',//动态路由的配置
        name: '123',//命名路由的配置
        component: Home
      },
     {
        path: '/helloword',
        component: Helloworld
      },
     {
        path: '*',
        redirect: '/home'
      }/*默认跳转路由*/   
    ]

5.实例化vue-router:在router文件夹下的index.js
 

   const router = new Router({
     mode:"history",//把hash模式改为history模式
     routes // (缩写)相当于 routes: routes
   })

6.暴露模块:在router文件夹下的index.js

  export default router

6.挂载:在main.js文件中
  

 new Vue({
   el: '#app',  
   router,  
   render: h => h(App)  
 })

使用

  <router-link to='/home'>去home</router-link>
  this.$router.push({path:'helloworld'})
  this.$router.push('/helloworld')
  this.$router.push({ name: '123' });//命名路由
  <router-link to='/home/123'>去home</router-link>//动态路由
  <router-link :to="'/home/'+key">去home</router-link>//动态路由
    geturl(){
        console.log(this.$route.params);//aid:123
    }
  <router-link to='/home?aid=123'>去home</router-link>//get传值
    geturl(){
        console.log(this.$route.query);//aid:"123"
    }
  <router-link :to="'/home?aid='+key">去home</router-link>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值