路由综合运用

Router & Route

实验题目
页面导航
实验目的
掌握普通路由、命名路由、重定向路由和嵌套路由的基本使用
掌握单页路由、全站路由的设计与开发
掌握编程路由和路由守卫的常见应用
路由样式的使用
实验内容
单页路由
全站路由
面包屑导航
参考效果和参考代码
基本步骤

.创建项目

.创建SFC

.创建路由

单页路由
routes: [
  {
    path: '/',
    name: 'home',
    component: Home,
    meta: {title: "home"}
  },
  {
    path: '/home',
    redirect: '/',
    meta: {title: "home"}
  },
  {
    path: '/team',
    name: 'team',
    component: () => import('../components/sub_view/Team.vue'),
    meta: {title: "team"}
  },
  {
    path: '/work',
    name: 'work',
    component: () => import('../components/sub_view/Work.vue'),
    meta: {title: "work"}
  },
  {
    path: '/about',
    name: 'about',
    component: () => import('../components/sub_view/About.vue'),
    meta: {title: "about"}
  },
  {
    path: '/:pathMatch(.*)*',
    name: 'NotFound',
    component: () => import('../components/NotFound.vue'),
    meta: {title: "404"}
  }
]
全站路由
routes: [
  {
    path: '/',
    name: 'login',
    component: Login,
  },
  {
    path: '/login',
    redirect: '/'
  },
  {
    path: '/home',
    name: 'home',
    component: () => import('../views/Home.vue'),
    children: [
      {
        path: 'user',
        name: 'user',
        component: () => import('../views/User.vue')
      },
      {
        path: 'good',
        name: 'good',
        component: () => import('../views/Good.vue')
      },
      {
        path: 'cart',
        name: 'cart',
        component: () => import('../views/Cart.vue')
      },
      {
        path: 'order',
        name: 'order',
        component: () => import('../views/Order.vue')
      },
      {
        path: 'todo',
        name: 'todo',
        component: () => import('../views/Todo.vue')
      },
      {
        path: 'setup',
        name: 'setup',
        component: () => import('../views/Setup.vue')
      },
      {
        path: 'about',
        name: 'about',
        component: () => import('../views/About.vue')
      }
    ]
  },
  {
    path: '/:pathMatch(.*)*',
    name: 'NotFound',
    component: () => import('../components/NotFound.vue')
  }
]
面包屑导航

.创建组件 Breadcrumb.vue

.引入路由

import { useRoute } from 'vue-router';
const route = useRoute();

.使用路由

<div class="bread">
<router-link v-for="(item, index) in route.matched" :key="index" :to="item.path">{{ item.name }}</router-link>
</div>

.仅仅使用路由信息渲染面包屑

<div class="bread">
<div v-for="(item, index) in route.matched" :key="index">{{ item.name }}</div>
</div>

.样式 - 略

.使用组件 - 在父组件中使用;略

拓展与思考
根据需要,使用动态路由、查询路由或传参路由传递参数
说明

现场验收、课后提交实验报告

使用浏览器访问静态资源,如图片、视频、音频等
在开发环境中获取数据,如 JSON 数据