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

使用 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')}]
}

http://www.xdnf.cn/news/4649.html

相关文章:

  • 如何通过服务主体获取 Azure 凭据
  • Ansible 流程控制
  • MySQL的索引和事务
  • @AutoConfigureBefore功能简介-笔记
  • ideal创建Springboot项目(Maven,yml)
  • 在Git历史中移除现有的Commit
  • Python 异常处理与文件 IO 操作:构建健壮的数据处理体系(3/10)
  • 高低比率策略
  • 天选5Pro(锐龙版)设备声音、显卡消失等问题完整解决记录
  • 表达式求值(算法题)
  • CMU-15445(3)——PROJECT#1-BufferPoolManager-Task#1
  • 【MySQL】存储引擎 - CSV详解
  • C++ stl中的string的相关用法
  • 【人工智能agent】--dify通过mcp协议调用工具
  • HR新战场:构建AI战略时代的认知分工与生态化人才供应链
  • 嵌入式C进阶路线指南
  • 创建虚拟服务时实现持久连接。
  • [人机交互]交互设计过程
  • 堆排序(算法题)
  • Easy云盘总结篇-文件分享
  • 如何看待首个通用型智能体 (The First General AI Agent) Manus发布?
  • ORB-SLAM3论文阅读
  • Agent学习之:MCP和Function Call
  • # 创建一个功能完备的计算器应用:使用PyQt5和Python
  • 软考高级-系统设计架构师高频考点速记
  • 101个alpah学习,第3个alpha
  • C++初阶-string类4
  • Advanced Installer 22.5打包windows 安装包
  • 前端线上错误日志收集与定位指南
  • 食品行业EDI:General Mills EDI需求分析