diff --git a/src/hooks/router.ts b/src/hooks/router.ts index 7c335d0..9d4e9b6 100644 --- a/src/hooks/router.ts +++ b/src/hooks/router.ts @@ -1,11 +1,44 @@ import { Navigates } from '@/utils/router/navigates'; +import { useRouterStore } from '@/state/modules/router'; +import { RouteLocationNormalized } from '@/types/router/route'; -const navigates = new Navigates(); +const router = new Navigates(); +/** + * 路由hook + */ export function useRouter() { - return navigates; + return router; } -export function useRoute() { +/** + * 获取当前Route信息 + * 推荐在onLoad中调用此hook + * getCurrentPages方法在不同平台有差异 + * 在微信小程序中只有在onLoad中才能获取到query + * @return RouteLocationNormalized + */ +export function useRoute(): RouteLocationNormalized { const currentPages = getCurrentPages(); + const currentPage = currentPages[currentPages.length - 1]; + const path = currentPage?.route || ''; + const routerStore = useRouterStore(); + const currentRoute = routerStore.getRoutes?.get(path as string); + let query = {}; + /* #ifndef MP-WEIXIN */ + // @ts-ignore + query = currentPage?.$page?.options || {}; + /* #endif */ + + /* #ifdef MP-WEIXIN */ + // @ts-ignore + query = currentPage?.options || {}; + /* #endif */ + return { + currentPages, + currentPage, + path, + currentRoute, + query, + }; } diff --git a/src/pages.json b/src/pages.json index bf57756..b9907bd 100644 --- a/src/pages.json +++ b/src/pages.json @@ -58,11 +58,17 @@ "path": "list/test1/index", "style": { "navigationBarTitleText": "test1" + }, + "meta": { + "ignoreAuth": true } },{ "path": "list/test2/index", "style": { "navigationBarTitleText": "test2" + }, + "meta": { + "ignoreAuth": true } }] }, { @@ -71,6 +77,9 @@ "path": "detail/index", "style": { "navigationBarTitleText": "Detail" + }, + "meta": { + "ignoreAuth": true } }] }], diff --git a/src/pages/demo/index.vue b/src/pages/demo/index.vue index de16b13..90b9df4 100644 --- a/src/pages/demo/index.vue +++ b/src/pages/demo/index.vue @@ -1,5 +1,14 @@ diff --git a/src/pages/index/index.vue b/src/pages/index/index.vue index f3e5eff..cb71d86 100644 --- a/src/pages/index/index.vue +++ b/src/pages/index/index.vue @@ -2,9 +2,10 @@ import BasicButton from '@/components/BasicButton/index.vue'; import AppProvider from '@/components/AppProvider/inedx.vue'; import { ref } from 'vue'; - import { router } from '@/utils/router'; + import { useRouter } from '@/hooks/router'; const title = ref('uni-app vue3 ts --Vite'); - console.log('感受到考虑到垮落法'); + + const router = useRouter(); const handleGetStarted = () => { router.pushTab('/pages/demo/index?d=str'); }; diff --git a/src/pagesA/list/test1/index.vue b/src/pagesA/list/test1/index.vue index 7d260c5..0de2e64 100644 --- a/src/pagesA/list/test1/index.vue +++ b/src/pagesA/list/test1/index.vue @@ -1,3 +1,14 @@ - - + + diff --git a/src/pagesA/list/test2/index.vue b/src/pagesA/list/test2/index.vue index 7f85915..e51301c 100644 --- a/src/pagesA/list/test2/index.vue +++ b/src/pagesA/list/test2/index.vue @@ -1,3 +1,14 @@ - - + + diff --git a/src/pagesB/detail/index.vue b/src/pagesB/detail/index.vue index 53e96c8..0ffac32 100644 --- a/src/pagesB/detail/index.vue +++ b/src/pagesB/detail/index.vue @@ -1,3 +1,12 @@ - + diff --git a/src/state/modules/router.ts b/src/state/modules/router.ts index c8a3621..f717193 100644 --- a/src/state/modules/router.ts +++ b/src/state/modules/router.ts @@ -1,10 +1,10 @@ import { defineStore } from 'pinia'; -import { Pages } from '@/types/pages'; -import { pagesMap } from '@/utils/router/pages'; +import { Route } from '@/types/router/route'; +import { pagesMap } from '@/utils/router/routes'; interface routeStore { - routes: Map | undefined; - currentRouter: Pages | undefined; + routes: Map | undefined; + currentRouter: Route | undefined; } export const useRouterStore = defineStore({ diff --git a/src/types/pages.d.ts b/src/types/pages.d.ts deleted file mode 100644 index 958cea4..0000000 --- a/src/types/pages.d.ts +++ /dev/null @@ -1,15 +0,0 @@ -export interface Pages extends Record { - path: string; - meta?: { - ignoreAuth?: boolean; - }; - style: { - navigationBarTitleText: string; - [key: string]: string; - }; -} - -export interface SubPackages { - root: string; - pages: Pages[]; -} diff --git a/src/types/router/route.d.ts b/src/types/router/route.d.ts new file mode 100644 index 0000000..a721f39 --- /dev/null +++ b/src/types/router/route.d.ts @@ -0,0 +1,28 @@ +export interface Route extends Record { + path: string; + meta?: { + ignoreAuth?: boolean; + }; + style: { + navigationBarTitleText: string; + [key: string]: string; + }; +} + +export interface SubPackages { + root: string; + pages: Route[]; +} + +export interface RouteLocationNormalized { + /* 当前页面栈的实例 */ + currentPages: Page.PageInstance[]; + /* 当前页面的实例 */ + currentPage: Page.PageInstance; + /* 当前页面在pages.json中的配置 */ + currentRoute?: Route; + /* 当前页面的path */ + path?: string; + /* 当前页面的url参数 */ + query: Record; +} diff --git a/src/utils/router/navigates.ts b/src/utils/router/navigates.ts index 8243545..d3dd472 100644 --- a/src/utils/router/navigates.ts +++ b/src/utils/router/navigates.ts @@ -46,7 +46,7 @@ export class Navigates { this.navigate(url, options); } replaceAll(url: string, options?: NavigateOptions) { - this.type = NAVIGATE_TYPE.REDIRECT_TO; + this.type = NAVIGATE_TYPE.RE_LAUNCH; this.navigate(url, options); } pushTab(url: string, options?: NavigateOptions) { diff --git a/src/utils/router/pages.ts b/src/utils/router/routes.ts similarity index 69% rename from src/utils/router/pages.ts rename to src/utils/router/routes.ts index cdbb992..d7db622 100644 --- a/src/utils/router/pages.ts +++ b/src/utils/router/routes.ts @@ -1,13 +1,13 @@ import pagesJson from '@/pages.json'; -import { Pages } from '@/types/pages'; +import { Route } from '@/types/router/route'; const { pages, subPackages } = pagesJson; // 将pages.json转换成Map对象,path为key -const pagesMap = new Map(); +const pagesMap = new Map(); pages.forEach((page) => { - pagesMap.set(page.path, page as Pages); + pagesMap.set(page.path, page as Route); }); if (Array.isArray(subPackages) && subPackages.length) { @@ -15,7 +15,7 @@ if (Array.isArray(subPackages) && subPackages.length) { const rootPath = el.root; el.pages.forEach((page) => { page.path = rootPath + '/' + page.path; - pagesMap.set(page.path, page as Pages); + pagesMap.set(page.path, page as Route); }); }); }