{"id":16039979,"url":"https://github.com/sonofmagic/weapp-navigate","last_synced_at":"2025-06-24T22:34:21.292Z","repository":{"id":179696989,"uuid":"663991817","full_name":"sonofmagic/weapp-navigate","owner":"sonofmagic","description":"weapp-navigate","archived":false,"fork":false,"pushed_at":"2025-02-04T06:27:09.000Z","size":316,"stargazers_count":3,"open_issues_count":11,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-05-15T09:44:24.292Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/sonofmagic.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-07-08T16:14:46.000Z","updated_at":"2024-08-09T04:03:26.000Z","dependencies_parsed_at":null,"dependency_job_id":"cfec739c-ad3b-4e9b-a857-b718798802bc","html_url":"https://github.com/sonofmagic/weapp-navigate","commit_stats":null,"previous_names":["sonofmagic/weapp-navigate"],"tags_count":3,"template":false,"template_full_name":"sonofmagic/npm-lib-template","purl":"pkg:github/sonofmagic/weapp-navigate","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sonofmagic%2Fweapp-navigate","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sonofmagic%2Fweapp-navigate/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sonofmagic%2Fweapp-navigate/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sonofmagic%2Fweapp-navigate/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sonofmagic","download_url":"https://codeload.github.com/sonofmagic/weapp-navigate/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sonofmagic%2Fweapp-navigate/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":260154525,"owners_count":22967132,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-08T23:09:20.050Z","updated_at":"2025-06-24T22:34:21.269Z","avatar_url":"https://github.com/sonofmagic.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# weapp-navigate\n\n[![codecov](https://codecov.io/gh/sonofmagic/weapp-navigate/branch/main/graph/badge.svg?token=EgsFF9UMt5)](https://codecov.io/gh/sonofmagic/weapp-navigate)\n\n\u003e 小程序框架路由跳转方法\n\n- [weapp-navigate](#weapp-navigate)\n  - [Features](#features)\n  - [Usage](#usage)\n    - [基础使用](#基础使用)\n    - [进阶使用](#进阶使用)\n      - [创建自定义hook](#创建自定义hook)\n  - [Options](#options)\n    - [`createNavigate`](#createnavigate)\n    - [`navigate`](#navigate)\n  - [License](#license)\n\n## Features\n\n- `promisify` 的 `api` 调用\n- 创建拦截器控制路由跳转(用于鉴权等等)\n- 多框架统一的 `api` 管理\n- 参数组合\n\n## Usage\n\n```bash\n\u003cnpm|yarn|pnpm\u003e i weapp-navigate\n```\n\n### 基础使用\n\n```js\nimport { navigate, createNavigate } from 'weapp-navigate'\n\nasync function main() {\n  // 使用\n  await navigate({\n    url: '/pages/index/index',\n    params: {\n      id: '1',\n      type: 'add'\n    }\n  })\n  // 相当于\n  await wx.navigateTo({\n    url: '/pages/index/index?id=1\u0026type=add'\n  })\n\n  await navigate({\n    type: 'navigateBack'\n  })\n  // 相当于\n  await wx.navigateBack()\n\n  await navigate({\n    type: 'reLaunch',\n    url: '/pages/index/index'\n  })\n  // 相当于\n  await wx.reLaunch({\n    url: '/pages/index/index'\n  })\n  // 同样还有 redirectTo 和 switchTab\n}\n```\n\n### 进阶使用\n\n#### 创建自定义hook\n\n```js\nimport { createNavigate } from 'weapp-navigate'\n\nconst navigate = createNavigate({\n  // 默认情况下使用全局 wx 对象，你可以使用 uni / taro 这种对象\n  globalTarget: uni,\n  onBeforeExecute: async (options) =\u003e {\n    // 这里你可以对参数进行修改\n    if (options.type === 'navigateTo') {\n      options.url = options.url + '54321'\n    }\n    // 或者你可以使用异步方法，进行远程调用\n    const isAuth = await getPermission()\n    \n    if(isAuth){\n      // 在这里抛出错误之后,不会去执行 navigateTo 那些方法，同时这个错误会被 onError 捕获\n      throw new Error('no login !')\n    }\n  },\n  onAfterExecute(result) {\n    // onAfterExecute 可以对 result 返回值进行修改\n    result.errMsg = '111'\n    // 同样也可以在这里使用异步方法，或者抛出错误\n    // throw new Error('no permission')\n  },\n  onError(error) {\n    console.error(error)\n  }\n})\nconst result = await navigate({\n  url\n})\n```\n\n## Options\n\n### `createNavigate`\n\n[types](./src/type.ts)\n\n```ts\nexport interface ICreateNavigateOptions {\n  globalTarget?: IGlobalTarget\n  onError?: (err: Error) =\u003e void\n  onBeforeExecute?: (options: NavigateOptions) =\u003e void | Promise\u003cvoid\u003e\n  onAfterExecute?: (result: CallbackResult) =\u003e void | Promise\u003cvoid\u003e\n}\n\n```\n\n### `navigate`\n\n[types](./src/type.ts)\n\n```ts\nNavigateOptions\n```\n\n## License\n\nThis project uses the [MIT license](./LICENSE).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsonofmagic%2Fweapp-navigate","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsonofmagic%2Fweapp-navigate","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsonofmagic%2Fweapp-navigate/lists"}