当前位置: 首页 > news >正文

vue3+ts+vite自适应项目——路由、layout布局

系列文章目录

第一章:搭建项目


目录

系列文章目录

前言

一、vue-router

1.安装vue-router

2.引入

2.1 新建页面

2.2 公共样式引入

2.3 layout 布局

2.4路由配置

总结


前言

上一章我们搭建了项目,这一张主要讲路由和layout布局,和vue2 有所不同,但是也有相似的地方,如果看过我之前的博客,那这里就很好理解啦~


一、vue-router

1.安装vue-router

首先,我们需要安装vue-router,执行以下命令:

yarn add vue-router

2.引入

2.1 新建页面

首先,我们新建views文件夹,在新建三个页面,如下:

每个页面先只写一个div占位,如下:

<template><div class="comm_bg">首页</div>
</template>
<script lang="ts" setup></script>
<style scoped lang="scss"></style>

2.2 公共样式引入

上一章我们知道啦如何 引入公共样式 和 修改element-plus 样式,这里我们需要定义另一个index.scss 存放两个文件,结构如下:

 在index.scss里面引入两个文件,如下:

@import './element.scss';
@import './public.scss';

修改vite.config.ts 中得公共样式路径,如下:

 此时,我们在 public.scss 中定义comm_bg 作为每个页面得公共背景类,如下:

.comm_bg {width: auto;height: 100%;padding: 29px;background: #ffffff;border-radius: 14px;
}

2.3 layout 布局

新建layout/index.vue 和 header.vue,结构如下:

 header.vue如下:

<template><div class="barBg"><div class="left">XXX综合平台</div><div class="right"><div class="btn" :class="nowIndex === index ? 'actived' : ''" v-for="(item, index) in tabList" :key="item.id"@click="clickTab(item.router, index)"><span>{{ item.name }}</span></div></div></div>
</template><script lang="ts" setup>
// 引入路由
import { useRouter } from 'vue-router'
import { ref } from 'vue';
//全局中引入这个方法
const router = useRouter()
const nowIndex = ref(0)
console.log(nowIndex.value)
//声明一个函数
const clickTab = (e, index) => {console.log(e)nowIndex.value = indexrouter.push(e)
}
const tabList = [{ id: "1", name: '首页', router: '/home', },{ id: "3", name: '综合商城', router: '/mine', },{ id: "2", name: '我的主页', router: '/mine', },]</script><style lang="scss" scoped>
.barBg {padding: 50px 3px;display: flex;justify-content: space-between;.left {font-size: 28px;line-height: 19px;color: #ffffff;}.actived {padding-bottom: 23px;border-bottom: 4px solid #ffff;border-radius: 2px;font-weight: bold}.right {display: flex;justify-content: space-between;.btn {display: flex;vertical-align: middle}div {margin-right: 29px;}div:nth-child(3) {margin-right: 0;}}}
</style>

index.vue主要是配置layout布局,如下:

<template><el-container><el-header><Bar /></el-header><el-main><!-- 子路由出口 --><router-view /></el-main></el-container>
</template><script setup lang="ts">
import Bar from './header.vue'</script><style scoped lang="scss">
.el-container {height: 100vh;
}.el-header {width: calc(100vw);height: 128px;background-color: $main_bg;color: #ffffff
}.el-main {width: calc(100vw);background-color: #E9EEF3;padding: 30px;
}
</style>

2.4路由配置

我们新建 router/routes.ts  引入layout布局,主要存放路由信息,代码如下:


// 引入创建路由模式 history模式
import Layout from '../layout/index.vue'
// 引入路由各页面配置
const routes = [{path: '/login',name: 'login',component: () => import('../views/login/index.vue'),},{path: '/',redirect: '/home'},{path: '/',component: Layout,name: 'layout',children: [{path: '/home',component: () => import('../views/home/index.vue'),name: 'home'},{path: '/mine',component: () => import('../views/mine/index.vue'),name: 'mine'}]},]export default routes;

再新建router/index.ts,注册路由,这里使用history模式,如下:

import { createRouter, createWebHistory } from "vue-router"
import routes from "./routes"
var router = createRouter({history: createWebHistory(),routes
})
export default router  

最后在main.ts里面引入 和注册,如下:

 此时路由就引入完成了~

最后再配置app.vue入口,如下:

<script setup lang="ts"></script><template><router-view />
</template><style lang="scss" scoped></style>

此时再运行,截图如下:


总结

以上就是今天得内容,路由配置,下一章准备将请求控制~

http://www.lryc.cn/news/63470.html

相关文章:

  • 数据库之约束、索引和事务
  • centos --libreoffice使用
  • Steam-V Rising 私人服务器架设教程
  • SpringBoot+Vue3实现登录验证码功能
  • spring2:创建和使用
  • 前端如何处理后端一次性传来的10w条数据?
  • Codeforces Round 867 (Div. 3)(A-G2)
  • 蓝奥声核心技术分享——一种无线低功耗配置技术
  • kafka集群模拟单节点故障
  • 笔记:vue-cli-service
  • Amazon S3 对象存储Java API操作记录(Minio与S3 SDK两种实现)
  • ChatGPT技术原理 第六章:对话生成技术
  • 【C++ 八】写文件、读文件
  • 【学习笔记】CF613E Puzzle Lover
  • 软考报名资格审核要多久?证明材料要哪些?
  • 2023-04-27 polardbx-LSM-tree的Parallel Recovery性能优化
  • 创作纪念日让 AI 与我共同记录下今天 — 【第五周年、1460天】
  • 枚举法计算24点游戏
  • @Cacheable注解
  • CentOS分区挂载 fdisk、parted方式解析
  • BuildKit
  • c++ 11标准模板(STL) std::vector (二)
  • Python 循环技巧
  • 【Java笔试强训 7】
  • 工作7年的程序员,明白了如何正确的“卷“
  • 数学建模——查数据
  • PAT A1019 General Palindromic Number
  • ChatGPT会颠覆SEO内容创作吗
  • Maven私服搭建
  • Ajax和Json综合案例