# 第一次使用
- 1:安装 `npm install vue-router -D`
>注意vue2.x支持 vue-router@3 vue3.x支持 vue-route@4
- 2: 初始化路由对象
- 1:创建独立的文件
- 2:编写路由实例对象代码
- 3:将路由对象挂在到应用对象上 (main。js)
> 如果不挂在,不能再当前项目中使用路由的。
## 如何使用路由实现单页面应用
- 1:搭建导航条
- RouterLink
- RouterView
- 2:编写路由配置
## router@4中几种路由模式
- createWebHistory()
- createWebHashHistory()
1.vite.config.js 文件
import { fileURLToPath, URL } from 'node:url'
import { defineConfig } from 'vite'
//vite 的插件 作用:打包编译.vue文件
import vue from '@vitejs/plugin-vue'
// https://vitejs.dev/config/
export default defineConfig({
//https://vitejs.dev/config/
plugins: [vue()], //使用插件
resolve: {
alias: {
'@': fileURLToPath(new URL('./src', import.meta.url)),
'api':fileURLToPath(new URL('./src/api',import.meta.url))
}
}
})