Skip to content

安装

前置条件

  • Node.js >= 16
  • uni-app 项目(基于 Vue 3)
  • pnpm / npm / yarn

安装依赖

bash
pnpm add @meng-xi/uni-router
bash
npm install @meng-xi/uni-router
bash
yarn add @meng-xi/uni-router

对等依赖

@meng-xi/uni-routervue 声明为可选的对等依赖。如果你的项目已经安装了 Vue 3,则无需额外操作。如果未安装,需要确保项目中有 Vue 3:

json
{
  "dependencies": {
    "vue": "^3.0.0"
  }
}

TIP

vue 被标记为可选依赖,因为路由核心逻辑不依赖 Vue 运行时。仅在需要使用 useRouter() / useRoute() 组合式 API 时才需要 Vue 的 inject 功能。

TypeScript 支持

@meng-xi/uni-router 使用 TypeScript 编写,内置完整的类型定义,无需额外安装 @types 包。

确保你的 tsconfig.json 包含以下配置:

json
{
  "compilerOptions": {
    "moduleResolution": "bundler",
    "types": ["@dcloudio/types"]
  }
}

INFO

@dcloudio/types 提供了 uni-app 全局 API 的类型声明。如果你的项目尚未安装,可通过 pnpm add -D @dcloudio/types 安装。

验证安装

创建路由器实例并检查是否正常工作:

ts
import { createRouter } from '@meng-xi/uni-router'

const router = createRouter({
  routes: [
    { path: 'pages/index/index', name: 'home' }
  ]
})

console.log(router.currentRoute.path)

如果控制台输出 /(初始路由路径),则说明安装成功。

Released under the MIT License.