16-路由

154 阅读2分钟

vue-router 安装和配置的步骤

① 安装 vue-router 包

npm i vue-router@3.5.3 -S

② 创建路由模块

在 src 源代码目录下,新建 router/index.js 路由模块,并初始化如下的代码:

//src/router/index.js 就是当前项目的路由模块
// 导入 Vue 和 VueRouter 的包
import Vue from 'vue'
import VueRouter from 'vue-router'

// 调用 Vue.use() 函数,把 VueRouter 安装为 Vue 的插件
Vue.use(VueRouter)

// 创建路由的实例对象
const router = new VueRouter()

// 向外共享路由的实例对象
export default router

③ 导入并挂载路由模块 在main.js中设置

import Vue from 'vue'
import App from './App.vue'
// 导入路由模块,目的:拿到路由的实例对象
import router  from '@/router/index.js'

Vue.config.productionTip = false

new Vue({
  render: h => h(App),
  // 在 Vue 项目中,要想把路由用起来,必须把路由实例对象,通过下面的方式进行挂载
  // router:路由的实例对象
  router:router
}).$mount('#app')

④ 声明路由链接和占位符

router/index.js

//src/router/index.js 就是当前项目的路由模块
// 导入 Vue 和 VueRouter 的包
import Vue from 'vue'
import VueRouter from 'vue-router'
// 引入
import Home from '@/components/Home.vue'
import Movie from '@/components/Movie.vue'
import About from '@/components/About.vue'

// 调用 Vue.use() 函数,把 VueRouter 安装为 Vue 的插件
Vue.use(VueRouter)

// 创建路由的实例对象
const router = new VueRouter({
  // routes 是一个数组,作用:定义 hash 地址与组件之间的对应关系
  routes:[
    {path:'/home',component:Home},
    {path:'/movie',component:Movie},
    {path:'/about',component:About}
  ]
})

// 向外共享路由的实例对象
export default router

App.vue

<template>
  <div class="box">
    <h1>App 根组件</h1>
    <hr>
    <a href="#/home">首页</a>
    <a href="#/movie">电影</a>
    <a href="#/about">关于</a>
    <hr>
    <!-- 只要在项目中安装和配置了 vue-router,就可以使用 router-view 这个组件了 -->
    <!-- 作用:占位符 -->
    <router-view></router-view>
  </div>
</template>

<script>
export default {
}
</script>

<style>

</style>

使用router-link代替a链接

<template>
  <div class="box">
    <h1>App 根组件</h1>
    <hr>
    <!-- 当安装和配置了 vue-router 后,就可以使用 router-link 来替代普通的 a 链接了 -->
    <!-- <a href="#/home">首页</a> -->
    <!-- <a href="#/movie">电影</a> -->
    <!-- <a href="#/about">关于</a> -->
    <router-link to="/home">首页</router-link>
    <router-link to="/movie">电影</router-link>
    <router-link to="/about">关于</router-link>
    <hr>
    <!-- 只要在项目中安装和配置了 vue-router,就可以使用 router-view 这个组件了 -->
    <!-- 作用:占位符 -->
    <router-view></router-view>
  </div>
</template>

<script>
export default {
}
</script>

<style>

</style>

路由重定向

路由重定向指的是:用户在访问地址A的时候,强制用户跳转到地址B,从而展示特定的组件页面。

通过路由规则的 redirect 属性,指定一个新的路由地址,可以很方便地设置路由的重定向:

chrome_eXuh5gP8Xm

嵌套路由

通过路由实现组件的嵌套展示,叫做嵌套路由。

chrome_K3uneDKMqK

声明子路由链接和子路由占位符

在 About.vue 组件中,声明 tab1 和 tab2 的子路由链接以及子路由占位符。示例代码如下:

chrome_jLmKq2IqJ7

通过 children 属性声明子路由规则

在 src/router/index.js 路由模块中,导入需要的组件,并使用 children 属性声明子路由规则:

chrome_DbDkDsndyg

//src/router/index.js 就是当前项目的路由模块
// 导入 Vue 和 VueRouter 的包
import Vue from 'vue'
import VueRouter from 'vue-router'
// 引入需要的组件
import Home from '@/components/Home.vue'
import Movie from '@/components/Movie.vue'
import About from '@/components/About.vue'
import Tab1 from '@/components/tabs/Tab1.vue'
import Tab2 from '@/components/tabs/Tab2.vue'

// 调用 Vue.use() 函数,把 VueRouter 安装为 Vue 的插件
Vue.use(VueRouter)

// 创建路由的实例对象
const router = new VueRouter({
  // routes 是一个数组,作用:定义 hash 地址与组件之间的对应关系
  routes:[
    {path:'/',redirect:'/home'},
    {path:'/home',component:Home},
    {path:'/movie',component:Movie},
    {path:'/about',component:About,children:[
      // 子路由规则
      {path:'tab1',component:Tab1},
      {path:'tab2',component:Tab2}
    ]}
  ]
})

// 向外共享路由的实例对象
export default router

about.vue

<template>
  <div class="about-container">
    <h3>About 组件</h3>
    <router-link to="/about/tab1">tab1</router-link>
    <router-link to="/about/tab2">tab2</router-link>
    <hr>
    <router-view></router-view>
  </div>
</template>

<script>
export default {
  name: 'About'
}
</script>

<style lang="less" scoped>
.about-container {
  min-height: 200px;
  background-color: skyblue;
  padding: 15px;
  > a {
    margin-right: 10px;
  }
}
</style>

默认子路由

{	path:'/about',
	component:About,
	children:[
      // 子路由规则
      //默认子路由,如果 children 数组中,某个路由规则的 path 值为空字符串,则这条路由规则,叫做‘默认子路由’
      {path:'tab1',component:Tab1},
      {path:'',component:Tab2}
    ]}
<template>
  <div class="about-container">
    <h3>About 组件</h3>
    <router-link to="/about/tab1">tab1</router-link>
    <router-link to="/about/tab2">tab2</router-link>
    <hr>
    <router-view></router-view>
  </div>
</template>

动态路由

动态路由指的是:把 Hash 地址中可变的部分定义为参数项,从而提高路由规则的复用性。

在 vue-router 中使用英文的冒号(:)来定义路由的参数项。示例代码如下:

chrome_9UPi4eovu1

$route.params 参数对象

在动态路由渲染出来的组件中,可以使用 this.$route.params 对象访问到动态匹配的参数值。

chrome_HRhEdFlIoP

为路由规则开启 props 传参

开启 props 传参

//为路由规则开启 props 传参,从而方便的拿到动态参数的值
    {path:'/movie/:id',component:Movie,props:true}

接受 props 数据

<template>
  <div class="movie-container">
    <h3>Movie 组件 --- {{ this.$route.params.id }}</h3> //id前面就可以省略了
  </div>
</template>

<script>
export default {
  name: 'Movie',
  //----接受 props 数据----
  props:['id']
}
</script>

声明式导航 & 编程式导航

在浏览器中,点击链接实现导航的方式,叫做声明式导航。例如:

  • 普通网页中点击 链接、vue 项目中点击 都属于声明式导航

在浏览器中,调用 API 方法实现导航的方式,叫做编程式导航。例如:

  • 普通网页中调用 location.href 跳转到新页面的方式,属于编程式导航

vue-router 中的编程式导航 API

vue-router 提供了许多编程式导航的 API,其中最常用的导航 API 分别是:

① this.$router.push('hash 地址')

  • 跳转到指定 hash 地址,并增加一条历史记录

  • <template>
      <div>
        <button @click="gotoMovie">点击跳转到movie</button>
      </div>
    </template>
    
    <script>
    export default {
      methods:{
        gotoMovie(){
          this.$router.push('/movie/2')
        }
      }
    }
    </script>
    

② this.$router.replace('hash 地址')

  • 跳转到指定的 hash 地址,并替换掉当前的历史记录

③ this.$router.go(数值 n)

  • 实现导航历史前进、后退

  • <template>
      <div>
        <button @click="goback">后退</button>
      </div>
    </template>
    
    <script>
    export default {
      methods:{
        goback(){
          // go(-1)表示后退一层,如果后退的层数超过上限,则原地不动
          this.$router.go(-1)
        }
        
      }
    }
    </script>
    

$router.go 的简化用法

在实际开发中,一般只会前进和后退一层页面。因此 vue-router 提供了如下两个便捷方法:

① $router.back()

  • 在历史记录中,后退到上一个页面

② $router.forward()

  • 在历史记录中,前进到下一个页面