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

使用 Vite 创建 Vue 3 项目并手动配置路由的完整步骤

1. 创建 Vue 3 项目

首先确保你已经安装了 Node.js(建议版本 16+)和 npm/yarn/pnpm。

# 使用 npm
npm create vite@latest vue3-vite-router -- --template vue# 或使用 yarn
yarn create vite vue3-vite-router --template vue# 或使用 pnpm
pnpm create vite vue3-vite-router --template vue

2. 进入项目目录并安装依赖

cd vue3-vite-router# 安装基础依赖
npm install
# 或 yarn
yarn
# 或 pnpm
pnpm install# 安装 vue-router
npm install vue-router@4
# 或
yarn add vue-router@4
# 或
pnpm add vue-router@4

3. 配置路由

3.1 创建路由文件结构

src/
├── router/
│   └── index.js
├── views/
│   ├── HomeView.vue
│   ├── AboutView.vue
│   └── NotFoundView.vue
├── App.vue
└── main.js

3.2 配置路由文件 (src/router/index.js)

import { createRouter, createWebHistory } from 'vue-router'// 导入视图组件
import HomeView from '../views/HomeView.vue'const router = createRouter({history: createWebHistory(import.meta.env.BASE_URL),routes: [{path: '/',name: 'home',component: HomeView},{path: '/about',name: 'about',// 路由懒加载component: () => import('../views/AboutView.vue')},{// 404 页面path: '/:pathMatch(.*)*',name: 'not-found',component: () => import('../views/NotFoundView.vue')}]
})export default router

3.3 修改 main.js

import { createApp } from 'vue'
import App from './App.vue'
import router from './router'const app = createApp(App)app.use(router)app.mount('#app')

4. 创建视图组件

src/views/HomeView.vue

<template><div class="home"><h1>Home Page</h1><router-link to="/about">Go to About</router-link></div>
</template><script setup>
// 使用 script setup 语法
</script>

src/views/AboutView.vue

<template><div class="about"><h1>About Page</h1><router-link to="/">Go back Home</router-link></div>
</template><script setup>
</script>

src/views/NotFoundView.vue

<template><div class="not-found"><h1>404 - Page Not Found</h1><router-link to="/">Go back Home</router-link></div>
</template><script setup>
</script>

5. 修改 App.vue

<template><nav><router-link to="/">Home</router-link> |<router-link to="/about">About</router-link></nav><router-view />
</template><style>
#app {font-family: Avenir, Helvetica, Arial, sans-serif;text-align: center;color: #2c3e50;margin-top: 60px;
}nav {padding: 30px;
}nav a {font-weight: bold;color: #2c3e50;margin: 0 10px;
}nav a.router-link-exact-active {color: #42b983;
}
</style>

6. 运行项目

npm run dev
# 或
yarn dev
# 或
pnpm dev

7. 可选的高级配置

动态路由

// 在 router/index.js 中添加
{path: '/user/:id',name: 'user',component: () => import('../views/UserView.vue')
}

路由守卫

// 在 router/index.js 中添加
router.beforeEach((to, from, next) => {document.title = to.meta.title || 'My Vue App'next()
})

嵌套路由

{path: '/dashboard',name: 'dashboard',component: () => import('../views/DashboardView.vue'),children: [{path: 'profile',component: () => import('../views/DashboardProfile.vue')},{path: 'settings',component: () => import('../views/DashboardSettings.vue')}]
}

相关文章:

  • 蓝桥杯青少 图形化编程(Scratch)每日一练——校门外的树
  • 基于vueflow可拖拽元素的示例(基于官网示例的单文件示例)
  • 面试实践AND面经热点题目总结
  • 探索 C++23 的 views::cartesian_product
  • 基于机器学习的攻击检测与缓解,以及 SDN 环境中的多控制器布局优化
  • 微程序控制器的详细工作过程
  • 如何在Jmeter中调用C程序?
  • 深入理解Embedding Models(嵌入模型):从原理到实战(上)
  • 2025-05-08 Unity 网络基础9——FTP通信
  • 学习笔记:数据库——事务
  • 克里金模型+多目标优化+多属性决策!Kriging+NSGAII+熵权TOPSIS!
  • 使用Jmeter对AI模型服务进行压力测试
  • Matlab 四分之一车体被动和模糊控制对比
  • MySQL报错解决过程
  • MySQL 8.0 OCP 英文题库解析(一)
  • Python 爬虫之 XPath 元素定位
  • 【Linux】swap交换分区管理
  • 【ArcGIS微课1000例】0146:将多个文件夹下的影像移动到一个目标文件夹(以Landscan数据为例)
  • 一文读懂Python之requests模块(36)
  • 精品,架构师总结,MySQL 5.7 查询入门详解
  • 欧盟公布对美关税反制清单,瞄准美国飞机等产品
  • “三德子”赵亮直播间卖“德子土鸡”,外包装商标实为“德子土”
  • 化学家、台湾地区“中研院”原学术副院长陈长谦逝世
  • 公募基金行业迎系统性变革:基金公司业绩差必须少收费
  • 碧桂园服务:拟向杨惠妍全资持有的公司提供10亿元贷款,借款将转借给碧桂园用作保交楼
  • 胡祥|人工智能时代:文艺评论何为?