Skip to content
On this page

开发与热更

在开发环境下,我们常常面临的问题是在编写和调试页面 UI 时,热重载可能导致应用无法重新定位到原来的页面。以往的解决方式包括,将正在开发的页面放置在 pages.json -> pages 的首位,或者使用自定义编译来强制重定向到指定页面。

然而,在使用 uni-simple-router 时,由于嵌套路由的存在,传统的解决方式已不再适用。因此,我们提供了一个更简便的 API 来解决这个问题,并帮助你快速开发嵌套页面并在热重载时恢复到指定位置。

创建热刷新配置

你只需要在创建路由实例时,传递如下配置即可:

js
// router.js
type ModeRule = `development` | `production`

const platform = process.env.VUE_APP_PLATFORM!

const router = createRouter({
  // 其他配置
  hotRefresh:{
    mode:process.env.NODE_ENV as ModeRule,
    to:`/newPage?id=6666`,
    navType: platform.includes(`mp-`) ? `pushTab` : `replaceAll`
  },
})

在上述代码中,我们可以看到 hotRefresh 配置,该配置只会在开发环境下生效,在生产环境下不会运行。hotRefresh 中的 to 参数与调用 router.push 时的参数类似,用于指定要跳转的页面路径及参数。navType 参数用于指定跳转的类型,如果是在小程序下跳转到 原生的tabbar 页面时,你必须指定跳转类型为 pushTab

这样配置后,在开发环境下,当进行热重载时,会自动跳转到指定的页面。

hotRefresh 配置签名

ts
type NavTypeRule = 'push' | 'pushTab' | 'replace' | 'replaceAll' | 'back';

interface NavTypeRule {
  mode: `development` | `production`;
  to: RoutePushLocationRaw;
  navType: NavTypeRule;
}
开发与热更 has loaded