作者:手机用户2502880157 | 来源:互联网 | 2023-09-08 08:09
VUE3实战一之项目搭建1.项目初始化1.1.环境要求1.2.创建VUE项目-基于vite1.3.项目初始化2.项目配置2.1.基本配置修改iconTitle配置jsconfig.
VUE3实战一之项目搭建
- 1. 项目初始化
- 1.1. 环境要求
- 1.2. 创建VUE项目-基于vite
- 1.3. 项目初始化
- 2. 项目配置
- 2.1. 基本配置修改
- 2.2. 项目目录结构划分
- 2.3. css样式重置
- reset.css
- common.css
- index.css
- 3. 路由配置
- 3.1. router/index.js
- 3.2. 路由的view页面开发
- 3.3. main.js中只用路由
- 3.4. 视图中使用路由占位
- 3.5. 路由跳转
1. 项目初始化
1.1. 环境要求
node -v
v16.17.0
npm -v
8.15.0
1.2. 创建VUE项目-基于vite
npm init vue@latest
需要在初始化按照的模块可以按需按照,本次demo只取最简项目
![在这里插入图片描述](https://img.php1.cn/3cd4a/1eebe/cd5/70be2ca197098d98.webp)
1.3. 项目初始化
cd michael-demo
npm install
npm run dev
页面效果
![在这里插入图片描述](https://img.php1.cn/3cd4a/1eebe/cd5/ed19db63ee478b98.png)
项目目录
![在这里插入图片描述](https://img.php1.cn/3cd4a/1eebe/cd5/780a3060eeed6a4e.webp)
2. 项目配置
2.1. 基本配置修改
icon
![在这里插入图片描述](https://img.php1.cn/3cd4a/1eebe/cd5/1e3db12dd78db092.webp)
Title
![在这里插入图片描述](https://img.php1.cn/3cd4a/1eebe/cd5/8343fdbffb0056b5.webp)
配置jsconfig.json
{"compilerOptions": {"module": "esnext","baseUrl": "./","moduleResolution": "node","paths": {"@/*": ["src/*"]},"lib": ["esnext","dom","dom.iterable","scripthost"]},"vueCompilerOptions": {"experimentalDisableTemplateSupport": true}
}
2.2. 项目目录结构划分
删除自带的项目
![在这里插入图片描述](https://img.php1.cn/3cd4a/1eebe/cd5/8be1ccb5166feb93.webp)
按照常用的目录划分
还有个router存放路由相关的代码
![在这里插入图片描述](https://img.php1.cn/3cd4a/1eebe/cd5/e3aa5425383ba10d.png)
2.3. css样式重置
npm install normaliza
reset.css
body, h1, h2, h3, h4, ul, li {padding: 0;margin: 0;
}ul, li {list-style: none;
}a {text-decoration: none;color: #333;
}img {vertical-align: top;
}
common.css
:root {--primary-color: #ff9854;}body {font-size: 14px;}
index.css
@import "./common.css";
@import "./reset.css";
![在这里插入图片描述](https://img.php1.cn/3cd4a/1eebe/cd5/4283cd4bbba41b87.png)
3. 路由配置
3.1. router/index.js
import { createRouter, createWebHashHistory } from 'vue-router'const router = createRouter({history: createWebHashHistory(),routes: [{path: "/",redirect: "/home"},{path: "/home",component: () => import("@/views/home/home.vue")},{path: "/favor",component: () => import("@/views/favor/favor.vue")},{path: "/order",component: () => import("@/views/order/order.vue")},{path: "/message",component: () => import("@/views/message/message.vue")}]
})export default router
3.2. 路由的view页面开发
<template><div class&#61;"favor"><h2>favor</h2></div>
</template><script setup></script><style lang&#61;"less" scoped></style>
3.3. main.js中只用路由
![在这里插入图片描述](https://img.php1.cn/3cd4a/1eebe/cd5/72fd2c126203a875.webp)
3.4. 视图中使用路由占位
![在这里插入图片描述](https://img.php1.cn/3cd4a/1eebe/cd5/d84f9786330d9e41.png)
3.5. 路由跳转
<script setup></script><template><div><h2>vue</h2><router-view/></div><div class&#61;"app"><div class&#61;"router-link"><router-link to&#61;"/home">Home</router-link></div><div class&#61;"router-link"><router-link to&#61;"/favor">Favor</router-link></div><div class&#61;"router-link"><router-link to&#61;"/order">Order</router-link></div><div class&#61;"router-link"><router-link to&#61;"/message">Message</router-link></div></div>
</template><style lang&#61;"less" scoped>
.app {position: fixed;bottom: 0;left: 0;right: 0;height: 50px;display: flex;border-top: 1px solid #f3f3f3;.router-link {flex: 1;display: flex;flex-direction: column;justify-content: center;align-items: center;}}
</style>
![在这里插入图片描述](https://img.php1.cn/3cd4a/1eebe/cd5/a5d7215df572c386.webp)