路由的使用

路由的使用

路由重定向

redirect

javascript 复制代码
 {
        path: '/',
        // 通过这种方式,在访问/路径的时候会重定向到/login路径
        redirect: '/login'
    },
路由高亮

router-link-active

自定义 linkActiveClass

javascript 复制代码
//APP.vue
<style>
.router-link-active {
  background-color: aquamarine;
  font-size: 20px;
}

.my-active {
  background-color: aquamarine;
  font-size: 20px;
}
</style>

//src/router/index.js
const router = new VueRouter({
    mode: 'history',
    base: process.env.BASE_URL,
    routes,
    linkActiveClass:'my-active'
})

路由嵌套 ⻚面布局

  • 在路由里面加一个children属性,是一个数组,数组里面是路由对象, path里面不加'/',
  • 在嵌套的父组件里面加上router-view标签
javascript 复制代码
import Vue from 'vue'
import VueRouter from 'vue-router'
import ParentComp from '@/views/ParentComp.vue'
import login from '@/components/login.vue'
import registry from '@/components/registry.vue'

Vue.use(VueRouter)

const routes = [
    {
        path: '/',
        // 通过这种方式,在访问/路径的时候会重定向到/login路径
        redirect: '/parent'
    },
    {
        path: '/parent',
        component: ParentComp,
        // 涉及到了子路由的内容
        children: [
            {
                // 这就是用相对路径,相对于父组件的,这个实际路径就是/parent/login
                path: 'login',
                component: login
            },
            {
                // 这就是用相对路径,相对于父组件的,这个实际路径就是/parent/login
                path: 'registry',
                component: registry
            }
        ]
    }
]

const router = new VueRouter({
    mode: 'history',
    base: process.env.BASE_URL,
    linkActiveClass: 'my-active',
    routes
})

export default router
命名视图
  • 一个路径可以对应多个组件 属性components
javascript 复制代码
//APP.vue
<template>
  <!-- 通过这个路由的嵌套,我们可以实现一些布局 -->
  <div id="app">
    <!-- router-view可以设置名字,指定渲染的组件 -->
    <router-view></router-view>
    <div class='my-container'>
      <router-view name='sidebar'></router-view>
      <router-view name='main'></router-view>
    </div>
  </div>
</template>
<style>
  .router-link-active {
      background-color: aquamarine;
      font-size: 20px;
    }

    .my-active {
      background-color: aquamarine;
      font-size: 20px;
    }

    .header {
      width: 100%;
      height: 80px;
      background-color: red;
    }

    .my-container {
      height: calc(100vh - 80px);
    }

    .sidebar {
      height: 100%;
      float: left;
      width: 200px;
      background-color: gray;
    }

    .main {
      height: 100%;
      margin-left: 200px;
      background-color: khaki;
    }
</style>
javascript 复制代码
//src/router/index.js
import Vue from 'vue'
import VueRouter from 'vue-router'
import HeaderModel from '@/components/HeaderModel.vue'
import SidebarModel from '@/components/SidebarModel.vue'
import MainModel from '@/components/MainModel.vue'
// import TestModel from '@/components/TestModel.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    // 使用 components 可以定义多个组件
    components: {
      // 通过default属性可以设置默认的组件,默认组件只有一个,不写名字也可以渲染
      default: HeaderModel,
      sidebar: SidebarModel,
      MainModel
    },
    // children: [
    //     { path: 'test', component: TestModel }
    // ]
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  linkActiveClass: 'my-active',
  routes
})

export default router
相关推荐
崔庆才丨静觅5 小时前
hCaptcha 验证码图像识别 API 对接教程
前端
passerby60615 小时前
完成前端时间处理的另一块版图
前端·github·web components
掘了6 小时前
「2025 年终总结」在所有失去的人中,我最怀念我自己
前端·后端·年终总结
崔庆才丨静觅6 小时前
实用免费的 Short URL 短链接 API 对接说明
前端
崔庆才丨静觅6 小时前
5分钟快速搭建 AI 平台并用它赚钱!
前端
崔庆才丨静觅6 小时前
比官方便宜一半以上!Midjourney API 申请及使用
前端
Moment6 小时前
富文本编辑器在 AI 时代为什么这么受欢迎
前端·javascript·后端
崔庆才丨静觅7 小时前
刷屏全网的“nano-banana”API接入指南!0.1元/张量产高清创意图,开发者必藏
前端
剪刀石头布啊7 小时前
jwt介绍
前端
爱敲代码的小鱼7 小时前
AJAX(异步交互的技术来实现从服务端中获取数据):
前端·javascript·ajax