当前位置:首页 > 技术分析 > 正文内容

22《Vue 入门教程》VueRouter 路由嵌套

ruisui885个月前 (01-12)技术分析47

1. 前言

本小节我们介绍如何嵌套使用 VueRouter。嵌套路由在日常的开发中非常常见,如何定义和使用嵌套路由是本节的重点。同学们在学完本节课程之后需要自己多尝试配置路由。

2. 配置嵌套路由

实际项目中的应用界面,通常由多层嵌套的组件组合而成。同样地,URL 中各段动态路径也按某种结构对应嵌套的各层组件,例如:

/article/vue                          /article/react
+------------------+                  +-----------------+
| Article          |                  | Article         |
| +--------------+ |                  | +-------------+ |
| | Vue          | |  +------------>  | | React       | |
| |              | |                  | |             | |
| +--------------+ |                  | +-------------+ |
+------------------+                  +-----------------+
代码块12345678

借助 vue-router,使用嵌套路由配置,就可以很简单地表达这种关系。 在上一小节中我们学习了如何配置一个路由信息:

  {
    path: '路由地址',
    component: '渲染组件'
  }

要配置嵌套路由,我们需要在配置的参数中使用 children 属性:

  {
    path: '路由地址',
    component: '渲染组件',
    children: [
      {
        path: '路由地址',
        component: '渲染组件'
      }
    ]
  }

2.1 基本使用

接下来我们对上一小节的例子来做一个改造:在文章页面,我们对文章进行分类,提供两个链接按钮 vue、react,点击可以跳转到对应的文章列表,具体代码示例如下:

实例演示

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
</head>
<body>
  <div id="app">
    <div>
      <router-link to="/index">首页</router-link>
      <router-link to="/article">文章</router-link>
    </div>
    <router-view></router-view>
  </div>
</body>

<script src="https://unpkg.com/vue/dist/vue.js"></script>
<script src="https://unpkg.com/vue-router/dist/vue-router.js"></script>
<script type="text/javascript">

const Index = Vue.component('index', {
  template: '<div>Hello,欢迎使用慕课网学习 Vue 教程!</div>',
})

const Article = Vue.component('myArticle', {
  template: `<div>
      <div>
        <router-link to="/article/vue">vue</router-link>
        <router-link to="/article/react">react</router-link>
      </div>
      <router-view></router-view>
    </div>`,
})

const VueArticle = Vue.component('vueArticle', {
  template: `<ul><li>1. Vue 基础学习</li><li>2. Vue 项目实战</li></ul>`,
})

const ReactArticle = Vue.component('reactArticle', {
  template: `<ul><li>1. React 基础学习</li><li>2. React 项目实战</li></ul>`,
})

const routes = [
  { path: '/index', component: Index },
  { 
    path: '/article', 
    component: Article ,
    children: [
      {
        path: 'vue', 
        component: VueArticle ,
      },
      {
        path: 'react', 
        component: ReactArticle ,
      }
    ]
  }
]

const router = new VueRouter({
  routes: routes
})

  var vm = new Vue({
    el: '#app',
    router,
    data() {
        return {}
    }
  })
</script>
</html>

"运行案例" 可查看在线运行效果

代码解释: HTML 代码第 12-13 行,我们定义了两个跳转链接。 HTML 代码第 15 行,我们使用 <router-view></router-view> 组件来渲染匹配组件。 JS 代码第 5-7 行,我们定义了组件 Index。 JS 代码第 9-17 行,我们定义了组件 Article,组件内部使用 <router-link></router-link> 定义出来两个跳转链接,使用 <router-view></router-view> 来渲染匹配组件。 JS 代码第 19-21 行,我们定义了组件 VueArticle. JS 代码第 23-25 行,我们定义了组件 ReactArticle。 JS 代码第 27-43 行,我们定义了路由数组,在 ‘/article’ 中配置来嵌套路由 children JS 代码第 44-46 行,创建 router 实例,然后传 routes 配置。 JS 代码第 49 行,通过 router 配置参数注入路由。

2.2 定义路由地址

在上述的例子中,我们通过 ‘/article/vue’ 来访问嵌套路由,但是有时候你可能不希望使用嵌套路径,这时候我们可以对上面例子中的配置信息做一点修改:

const routes = [
  { path: '/index', component: Index },
  { 
    path: '/article', 
    component: Article ,
    children: [
      {
        path: '/vueArticle', 
        component: VueArticle ,
      },
      {
        path: '/reactArticle', 
        component: ReactArticle ,
      }
    ]
  }
]

‘/’ 开头的嵌套路径会被当作根路径,因此,我们访问 ‘/vueArticle’ 就相当于访问之前的 ‘/article/vue’。

3. 小结

本节,我们带大家学习了 VueRouter 嵌套路由的使用方法,主要知识点有以下几点:

  • 通过路由配置的 children 属性定义和使用嵌套路由。
  • 通过修改路由配置的 path 属性定义嵌套路由的跳转地址。

扫描二维码推送至手机访问。

版权声明:本文由ruisui88发布,如需转载请注明出处。

本文链接:http://www.ruisui88.com/post/206.html

标签: vue路由传值
分享给朋友:

“22《Vue 入门教程》VueRouter 路由嵌套” 的相关文章

适合旧电脑2022年值得推荐的 10 款轻量级 Linux 发行版

推荐 10 款轻量级Linux 发行版,它们是 2022 年的轻量级、对旧硬件友好的 Linux 发行版。1、Linux LiteLinux Lite 是一款基于#ubuntu# 和 Debian 的、正在不断开发和完善的 Linux 发行版,极好看的 Xfce 桌面,并基于 Ubuntu,采用了...

基于archlinux的发行版有哪些?

Arch Linux 是一个 Linux 发行版,采用滚动更新的模型,这意味着 Arch Linux 不会定期发布新版本,而是持续接收更新和升级,保持系统与最新软件版本的同步。Arch Linux 以其极简主义、简单性和用户定制为中心的特点而闻名,专注于让用户对其系统配置具有完全控制权。然而,它也以...

79.idea中git合并分支操作分享

文章目录前言1.fetch的操作2.合并最新代码到当前的开发分支3.解决冲突4.分支合并:5.完成代码合并总结前言git的操作在日产的工作中也非常重要,团队化的代码管理,每次如果代码被别人覆盖或者自己的代码不能提交到服务器那是灾难性的结果,本篇进行一篇分享来总结下idea中git的操作帮助java开...

js中数组filter方法的使用和实现

定义filter() 方法创建一个新数组, 其包含通过所提供函数实现的测试的所有元素。语法var newArray = arr.filter(callback(element[, index[, selfArr]])[, thisArg])参数callback循环数组每个元素时调用的回调函数。回调函...

Vue实战篇|使用路由管理用户权限(动态路由)

权限控制是后台管理系统比较常见的需求,如果我们需要对某些页面的添加权限控制的话,那我们可以在路由管理中的权限做一些校验,没有通过权限校验的给出相应的提示或者直接跳转到报错页面。跟着我一起来学vue实战篇路由管理权限吧!权限校验函数getCurrentAuthority()函数用于获取当前用户权限,一...

Vue中的路由配置常用属性

router:路由页面跳转的核心库;引入路由:import VueRouter from 'vue-router'; 注册路由:const router = new VueRouter({ })mode:模式路由有hash history两种模式:hash模式URL中包含#,#后边是...