diff --git a/packages/shims-uni-app.d.ts b/packages/shims-uni-app.d.ts index 43b1aacd0c63290b51aec04252cc3bf240535696..14e301fe6ddfdd108813760e9b1a9e2b3589840d 100644 --- a/packages/shims-uni-app.d.ts +++ b/packages/shims-uni-app.d.ts @@ -38,7 +38,7 @@ declare namespace UniApp { interface UniRoute { path: string - redirect?: string + alias?: string meta: PageRouteMeta component?: any } diff --git a/packages/uni-api/src/protocols/route/route.ts b/packages/uni-api/src/protocols/route/route.ts index bdd6fce5ca3e09f2d7932e9e4e0ff117ab74bb3d..58a47dcf54212e169f485b0bdfc5c34a71f9b903 100644 --- a/packages/uni-api/src/protocols/route/route.ts +++ b/packages/uni-api/src/protocols/route/route.ts @@ -133,9 +133,11 @@ function createNormalizeUrl(type: string) { url = getRealRoute(url) const pagePath = url.split('?')[0] // 匹配路由是否存在 - const routeOptions = __uniRoutes.find( - ({ path, redirect }) => path === pagePath || redirect === pagePath - ) + if (url === '/') { + // 首页 + url = __uniRoutes[0].path + } + const routeOptions = __uniRoutes.find(({ path }) => path === pagePath) if (!routeOptions) { return 'page `' + url + '` is not found' diff --git a/packages/uni-h5/dist/uni-h5.esm.js b/packages/uni-h5/dist/uni-h5.esm.js index 25b8a110cff4288827295e8df9cd5e66c221fc8d..c8b71ef93556a7863a85faf52b350e31d98c50f3 100644 --- a/packages/uni-h5/dist/uni-h5.esm.js +++ b/packages/uni-h5/dist/uni-h5.esm.js @@ -757,32 +757,17 @@ function createRouterOptions() { return { history: initHistory(), strict: !!__uniConfig.router.strict, - routes: [ - {path: __uniRoutes[0].path, redirect: "/"}, - ...__uniRoutes - ], + routes: __uniRoutes, scrollBehavior }; } -function initGuard(router) { - router.beforeEach(beforeEach); - router.afterEach(afterEach); -} function createAppRouter(router) { - initGuard(router); return router; } function initHistory() { const history2 = __UNI_FEATURE_ROUTER_MODE__ === "history" ? createWebHistory() : createWebHashHistory(); return history2; } -const beforeEach = (to, from, next) => { - next(); -}; -const afterEach = (to, from, failure) => { - console.log("afterEach.id", history.state.__id__); - console.log("afterEach", to, from, failure, JSON.stringify(history.state)); -}; var TabBar = /* @__PURE__ */ defineComponent({ name: "TabBar" }); @@ -912,7 +897,7 @@ function initPublicPage(route) { return { id, path: route.path, - route: normalizeRoute(route.meta.route || route.path), + route: normalizeRoute(route.path), fullPath: route.meta.isEntry ? route.meta.pagePath : route.fullPath, options: {}, meta: usePageMeta() @@ -8182,7 +8167,10 @@ function createNormalizeUrl(type) { return function normalizeUrl(url, params) { url = getRealRoute(url); const pagePath = url.split("?")[0]; - const routeOptions = __uniRoutes.find(({path, redirect}) => path === pagePath || redirect === pagePath); + if (url === "/") { + url = __uniRoutes[0].path; + } + const routeOptions = __uniRoutes.find(({path}) => path === pagePath); if (!routeOptions) { return "page `" + url + "` is not found"; } @@ -8378,27 +8366,25 @@ const navigateBack = defineAsyncApi(API_NAVIGATE_BACK, (options) => { } getApp().$router.go(-options.delta); }, NavigateBackProtocol, NavigateBackOptions); -const navigateTo = defineAsyncApi(API_NAVIGATE_TO, (options, callback) => { +function navigate(type, url, callback) { const router = getApp().$router; - router.push({ - path: options.url, + router[type === "navigateTo" ? "push" : "replace"]({ + path: url, force: true, - state: createPageState("navigateTo") + state: createPageState(type) }).then((failure) => { if (isNavigationFailure(failure)) { return callback({ - errMsg: `${API_NAVIGATE_TO}:fail ${failure.message}` + errMsg: `:fail ${failure.message}` }); } callback(); }); -}, NavigateToProtocol, NavigateToOptions); -const redirectTo = defineAsyncApi(API_REDIRECT_TO, () => { -}, RedirectToProtocol, RedirectToOptions); -const reLaunch = defineAsyncApi(API_RE_LAUNCH, () => { -}, ReLaunchProtocol, ReLaunchOptions); -const switchTab = defineAsyncApi(API_SWITCH_TAB, () => { -}, SwitchTabProtocol, SwitchTabOptions); +} +const navigateTo = defineAsyncApi(API_NAVIGATE_TO, (options, callback) => navigate(API_NAVIGATE_TO, options.url, callback), NavigateToProtocol, NavigateToOptions); +const redirectTo = defineAsyncApi(API_REDIRECT_TO, (options, callback) => navigate(API_REDIRECT_TO, options.url, callback), RedirectToProtocol, RedirectToOptions); +const reLaunch = defineAsyncApi(API_RE_LAUNCH, (options, callback) => navigate(API_RE_LAUNCH, options.url, callback), ReLaunchProtocol, ReLaunchOptions); +const switchTab = defineAsyncApi(API_SWITCH_TAB, (options, callback) => navigate(API_SWITCH_TAB, options.url, callback), SwitchTabProtocol, SwitchTabOptions); var api = /* @__PURE__ */ Object.freeze({ __proto__: null, [Symbol.toStringTag]: "Module", @@ -8570,8 +8556,9 @@ var PageHead = /* @__PURE__ */ defineComponent({ function createBackButtonTsx(navigationBar) { if (navigationBar.backButton) { return createVNode("div", { - class: "uni-page-head-btn" - }, [createSvgIconVNode(ICON_PATH_BACK, navigationBar.type === "transparent" ? "#fff" : navigationBar.titleColor, 27)]); + class: "uni-page-head-btn", + onClick: onPageHeadBackButton + }, [createSvgIconVNode(ICON_PATH_BACK, navigationBar.type === "transparent" ? "#fff" : navigationBar.titleColor, 27)], 8, ["onClick"]); } } function createButtonsTsx(btns) { @@ -8667,6 +8654,17 @@ function createPageHeadSearchInputTsx(navigationBar, { onInput }, null, 8, ["focus", "disabled", "style", "placeholder-style", "onFocus", "onBlur", "onInput"])], 4); } +function onPageHeadBackButton() { + if (getCurrentPages().length === 1) { + uni.reLaunch({ + url: "/" + }); + } else { + uni.navigateBack({ + from: "backbutton" + }); + } +} function usePageHead(navigationBar) { const clazz = computed(() => { const { diff --git a/packages/uni-h5/src/framework/components/page/pageHead.tsx b/packages/uni-h5/src/framework/components/page/pageHead.tsx index c558a6fed39f984fe363365583e07a12fadbef8a..f51fead30812f7fd1827210f412ddb0800743219 100644 --- a/packages/uni-h5/src/framework/components/page/pageHead.tsx +++ b/packages/uni-h5/src/framework/components/page/pageHead.tsx @@ -45,7 +45,6 @@ export default /*#__PURE__*/ defineComponent({ usePageHeadSearchInput(navigationBar)) as PageHeadSearchInput __UNI_FEATURE_NAVIGATIONBAR_TRANSPARENT__ && usePageHeadTransparent(headRef, navigationBar) - return () => { // 单页面无需back按钮 const backButtonTsx = __UNI_FEATURE_PAGES__ @@ -86,7 +85,7 @@ export default /*#__PURE__*/ defineComponent({ function createBackButtonTsx(navigationBar: UniApp.PageNavigationBar) { if (navigationBar.backButton) { return ( -
+
{createSvgIconVNode( ICON_PATH_BACK, navigationBar.type === 'transparent' @@ -206,6 +205,18 @@ function createPageHeadSearchInputTsx( ) } +function onPageHeadBackButton() { + if (getCurrentPages().length === 1) { + uni.reLaunch({ + url: '/', + }) + } else { + ;(uni.navigateBack as Function)({ + from: 'backbutton', + }) + } +} + function usePageHead(navigationBar: UniApp.PageNavigationBar) { const clazz = computed(() => { const { type, titlePenetrate, shadowColorType } = navigationBar diff --git a/packages/uni-h5/src/framework/plugin/page.ts b/packages/uni-h5/src/framework/plugin/page.ts index 65b8a147dcb8a5c8ba98127ca0df30d6df4fd1a5..757e834883565f0225fe27d06c300d8b490784c5 100644 --- a/packages/uni-h5/src/framework/plugin/page.ts +++ b/packages/uni-h5/src/framework/plugin/page.ts @@ -56,7 +56,7 @@ function initPublicPage(route: RouteLocationNormalizedLoaded) { return { id, path: route.path, - route: normalizeRoute((route.meta.route as string) || route.path), + route: normalizeRoute(route.path), fullPath: route.meta.isEntry ? route.meta.pagePath : route.fullPath, options: {}, // $route.query meta: usePageMeta(), diff --git a/packages/uni-h5/src/framework/plugin/router.ts b/packages/uni-h5/src/framework/plugin/router.ts index 8f7992a9314dccb54ddefb68181d9151d87ccf33..af9cee141acfa0ba605df86c09806fc9b5a7a963 100644 --- a/packages/uni-h5/src/framework/plugin/router.ts +++ b/packages/uni-h5/src/framework/plugin/router.ts @@ -1,11 +1,5 @@ import { App } from 'vue' -import { - Router, - RouterOptions, - RouteRecordRaw, - NavigationHookAfter, - NavigationGuardWithThis, -} from 'vue-router' +import { Router, RouterOptions, RouteRecordRaw } from 'vue-router' import { createRouter, createWebHistory, @@ -31,21 +25,12 @@ function createRouterOptions(): RouterOptions { return { history: initHistory(), strict: !!__uniConfig.router.strict, - routes: [ - { path: __uniRoutes[0].path, redirect: '/' }, - ...__uniRoutes, - ] as RouteRecordRaw[], + routes: (__uniRoutes as unknown) as RouteRecordRaw[], scrollBehavior, } } -function initGuard(router: Router) { - router.beforeEach(beforeEach) - router.afterEach(afterEach) -} - function createAppRouter(router: Router) { - initGuard(router) return router } @@ -54,22 +39,5 @@ function initHistory() { __UNI_FEATURE_ROUTER_MODE__ === 'history' ? createWebHistory() : createWebHashHistory() - // history.listen((_to, from, info) => { - // if (info.direction === 'back') { - // const app = getApp() - // const id = history.state.__id__ - // if (app && id) { - // ;(app.$refs.app as any).keepAliveExclude = [from + '-' + id] - // } - // } - // }) return history } - -const beforeEach: NavigationGuardWithThis = (to, from, next) => { - next() -} -const afterEach: NavigationHookAfter = (to, from, failure) => { - console.log('afterEach.id', history.state.__id__) - console.log('afterEach', to, from, failure, JSON.stringify(history.state)) -} diff --git a/packages/uni-h5/src/service/api/route/navigateTo.ts b/packages/uni-h5/src/service/api/route/navigateTo.ts index c500b8f1443fed78a7555d0fc4d3215613f06d5f..9a9e832243bf5c2ceb0e6f4a93a6b87d2cc5eb93 100644 --- a/packages/uni-h5/src/service/api/route/navigateTo.ts +++ b/packages/uni-h5/src/service/api/route/navigateTo.ts @@ -1,31 +1,15 @@ -import { isNavigationFailure, Router } from 'vue-router' import { API_NAVIGATE_TO, defineAsyncApi, NavigateToOptions, NavigateToProtocol, } from '@dcloudio/uni-api' -import { createPageState } from '../../../framework/plugin/page' +import { navigate } from './utils' export const navigateTo = defineAsyncApi( API_NAVIGATE_TO, - (options, callback?: Function) => { - const router = getApp().$router as Router - router - .push({ - path: options.url, - force: true, - state: createPageState('navigateTo'), - }) - .then((failure) => { - if (isNavigationFailure(failure)) { - return callback!({ - errMsg: `${API_NAVIGATE_TO}:fail ${failure.message}`, - }) - } - callback!() - }) - }, + (options, callback?: Function) => + navigate(API_NAVIGATE_TO, options.url, callback!), NavigateToProtocol, NavigateToOptions ) diff --git a/packages/uni-h5/src/service/api/route/reLaunch.ts b/packages/uni-h5/src/service/api/route/reLaunch.ts index bb0258f25faa50d56d89b81b68b1d66719023ea3..dbc34ed115f10239402586ac9731932084b2cb43 100644 --- a/packages/uni-h5/src/service/api/route/reLaunch.ts +++ b/packages/uni-h5/src/service/api/route/reLaunch.ts @@ -4,10 +4,12 @@ import { ReLaunchOptions, ReLaunchProtocol, } from '@dcloudio/uni-api' +import { navigate } from './utils' -export const reLaunch = defineAsyncApi( +export const reLaunch = defineAsyncApi( API_RE_LAUNCH, - () => {}, + (options, callback?: Function) => + navigate(API_RE_LAUNCH, options.url, callback!), ReLaunchProtocol, ReLaunchOptions ) diff --git a/packages/uni-h5/src/service/api/route/redirectTo.ts b/packages/uni-h5/src/service/api/route/redirectTo.ts index ff2d32328232b5a6e679fb551d7f06d1d2c95c92..f21db53e66cf51442fbb4fb5c6e3873b344ad6bb 100644 --- a/packages/uni-h5/src/service/api/route/redirectTo.ts +++ b/packages/uni-h5/src/service/api/route/redirectTo.ts @@ -4,10 +4,12 @@ import { RedirectToOptions, RedirectToProtocol, } from '@dcloudio/uni-api' +import { navigate } from './utils' -export const redirectTo = defineAsyncApi( +export const redirectTo = defineAsyncApi( API_REDIRECT_TO, - () => {}, + (options, callback?: Function) => + navigate(API_REDIRECT_TO, options.url, callback!), RedirectToProtocol, RedirectToOptions ) diff --git a/packages/uni-h5/src/service/api/route/switchTab.ts b/packages/uni-h5/src/service/api/route/switchTab.ts index 1280aac8a0f16f8b0c09d79e07cf0e7d6d4e801c..2eebd650a72e676b7753b5174d56cbb4af79ea3c 100644 --- a/packages/uni-h5/src/service/api/route/switchTab.ts +++ b/packages/uni-h5/src/service/api/route/switchTab.ts @@ -4,10 +4,12 @@ import { SwitchTabOptions, SwitchTabProtocol, } from '@dcloudio/uni-api' +import { navigate } from './utils' -export const switchTab = defineAsyncApi( +export const switchTab = defineAsyncApi( API_SWITCH_TAB, - () => {}, + (options, callback?: Function) => + navigate(API_SWITCH_TAB, options.url, callback!), SwitchTabProtocol, SwitchTabOptions ) diff --git a/packages/uni-h5/src/service/api/route/utils.ts b/packages/uni-h5/src/service/api/route/utils.ts new file mode 100644 index 0000000000000000000000000000000000000000..2cfe0b5210d2c5be84ea0911e6012d4b94593a19 --- /dev/null +++ b/packages/uni-h5/src/service/api/route/utils.ts @@ -0,0 +1,22 @@ +import { isNavigationFailure, Router } from 'vue-router' +import { createPageState } from '../../../framework/plugin/page' + +export function navigate( + type: 'navigateTo' | 'redirectTo' | 'reLaunch' | 'switchTab', + url: string, + callback: Function +) { + const router = getApp().$router as Router + router[type === 'navigateTo' ? 'push' : 'replace']({ + path: url, + force: true, + state: createPageState(type), + }).then((failure) => { + if (isNavigationFailure(failure)) { + return callback({ + errMsg: `:fail ${failure.message}`, + }) + } + callback() + }) +} diff --git a/packages/vite-plugin-uni/src/configResolved/plugins/pagesJson.ts b/packages/vite-plugin-uni/src/configResolved/plugins/pagesJson.ts index a29aa7265c0a0820654951debb17e352b7f0c8c0..42a7d81be74461b846972a179481a314ffd2c789 100644 --- a/packages/vite-plugin-uni/src/configResolved/plugins/pagesJson.ts +++ b/packages/vite-plugin-uni/src/configResolved/plugins/pagesJson.ts @@ -170,8 +170,10 @@ function normalizePagesRoute(pagesJson: UniApp.PagesJson): PageRouteOptions[] { } function generatePageRoute({ name, path, meta }: PageRouteOptions) { + const { isEntry } = meta + const alias = isEntry ? `\n alias:'/${path}',` : '' return `{ - path:'/${meta.isEntry ? '' : path}', + path:'/${isEntry ? '' : path}',${alias} component:{ render() { return (openBlock(), createBlock(PageComponent, null, {page: withCtx(() => [createVNode(${name})]), _: 1 /* STABLE */})) @@ -187,7 +189,6 @@ function generatePagesRoute(pagesRouteOptions: PageRouteOptions[]) { function generateRoutes(pagesJson: UniApp.PagesJson) { return `window.__uniRoutes=[${[ - `{ path: '/${pagesJson.pages[0].path}', redirect: '/' }`, ...generatePagesRoute(normalizePagesRoute(pagesJson)), ].join(',')}]` }