Vue路由切换

1. 相关API说明

VueRouter(): 用于创建路由器的构建函数

new VueRouter({
  // 多个配置项
})

路由配置

routes: [
  { // 一般路由
    path: '/about',
    component: about
  },
  { // 自动跳转路由
    path: '/', 
    redirect: '/about'
  }
]

组件:

1. router-link: 用来生成路由链接
<router-link to="/xxx">Go to XXX</router-link>
2. <router-view>: 用来显示当前路由组件界面
<router-view></router-view>

3. 实现简单路由

路由组件:

home.vue
about.vue

应用组件: App.vue

<div>
  <!--路由链接-->
  <router-link to="/about">About</router-link>
  <router-link to="/home">Home</router-link>
  <!--用于渲染当前路由组件-->
  <router-view></router-view>  
</div>

入口js: main.js

// 创建路由器(配置路由)
new VueRouter({
  routes: [
    {
      path: '/',
      redirect: '/about'
    },
    {
      path: '/about',
      component: about
    },
    {
      path: '/home',
      component: home
    }
  ]
})
  
// 创建vue配置路由器
new Vue({
  el: '#app',
  router,
  render: h => h(app)
})

优化路由器配置

linkActiveClass: 'active', // 指定选中的路由链接的class
mode: 'history',  // 路由路径不带#

4. 嵌套路由

配置嵌套路由

path: '/home',
component: home,
children: [
  {
    path: 'news',
    component: news
  },
  {
    path: 'message',
    component: message
  }
]

路由路径

<router-link to="/home/news">News</router-link>
<router-link to="/home/message">Message</router-link>

5. 向路由组件传递数据

路由路径携带参数

1.配置路由
  children: [
    {
      path: 'mdetail/:id',
      component: messageDetail
    }
  ]
2.路由路径
  <router-link :to="'/home/message/mdetail/'+m.id">{{m.title}}</router-link>
3.路由组件中读取请求参数
  this.$route.params.id

属性携带数据

<router-view :msg="msg"></router-view>

6. 其它重要

使用缓存路由组件

<keep-alive>
  <router-view></router-view>
</keep-alive>

路由

1. 分类

后台路由
前台路由

2. 是什么

就是一个映射关系(key:value)
后台路由: path: callback
前台路由: path: component 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值