@tarojs/router 4.0.0-beta.87 → 4.0.0-beta.89
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +8 -8
- package/dist/api.d.ts +7 -6
- package/dist/api.js +16 -4
- package/dist/events/resize.d.ts +3 -2
- package/dist/events/scroll.d.ts +3 -2
- package/dist/history.d.ts +8 -10
- package/dist/index.cjs.d.ts +66 -0
- package/dist/index.cjs.js +68 -84
- package/dist/index.d.ts +10 -9
- package/dist/index.esm.d.ts +66 -0
- package/dist/index.esm.js +66 -82
- package/dist/navigationBar.d.ts +3 -2
- package/dist/router/index.d.ts +4 -3
- package/dist/router/index.js +2 -3
- package/dist/router/mpa.d.ts +5 -4
- package/dist/router/mpa.js +2 -2
- package/dist/router/multi-page.d.ts +8 -7
- package/dist/router/navigation-bar.d.ts +3 -3
- package/dist/router/navigation-bar.js +6 -6
- package/dist/router/page.d.ts +10 -11
- package/dist/router/page.js +14 -26
- package/dist/router/spa.d.ts +5 -4
- package/dist/router/spa.js +23 -36
- package/dist/router/stack.d.ts +2 -2
- package/dist/style.d.ts +5 -4
- package/dist/style.js +4 -6
- package/dist/tabbar.d.ts +3 -2
- package/dist/utils/index.d.ts +3 -2
- package/dist/utils/navigate.d.ts +7 -6
- package/package.json +29 -18
package/LICENSE
CHANGED
|
@@ -154,8 +154,15 @@ See `/LICENSE` for details of the license.
|
|
|
154
154
|
|
|
155
155
|
==================
|
|
156
156
|
|
|
157
|
+
MIT (stencil-vue2-output-target):
|
|
158
|
+
The following files embed [stencil-vue2-output-target](https://github.com/diondree/stencil-vue2-output-target) MIT:
|
|
159
|
+
`/packages/taro-components-library-vue2/src/vue-component-lib/utils.ts`
|
|
160
|
+
See `/LICENSE` for details of the license.
|
|
161
|
+
|
|
162
|
+
==================
|
|
163
|
+
|
|
157
164
|
MIT (weui):
|
|
158
|
-
The following files embed [
|
|
165
|
+
The following files embed [stencil-vue2-output-target](https://github.com/Tencent/weui) MIT:
|
|
159
166
|
`/packages/taro-components/src/components/*.scss`
|
|
160
167
|
See `/LICENSE.txt` for details of the license.
|
|
161
168
|
|
|
@@ -165,10 +172,3 @@ Apache-2.0 (intersection-observer):
|
|
|
165
172
|
The following files embed [intersection-observer](https://github.com/GoogleChromeLabs/intersection-observer) Apache-2.0:
|
|
166
173
|
`/packages/taro-api/src/polyfill/intersection-observer.ts`
|
|
167
174
|
See `/LICENSE.txt` for details of the license.
|
|
168
|
-
|
|
169
|
-
==================
|
|
170
|
-
|
|
171
|
-
MIT (babel-plugin-jsx-dom-expressions):
|
|
172
|
-
The following files embed [babel-plugin-jsx-dom-expressions](https://github.com/ryansolid/dom-expressions/blob/main/packages/babel-plugin-jsx-dom-expressions) MIT:
|
|
173
|
-
`/packages/babel-plugin-transform-solid-jsx/src/*`
|
|
174
|
-
See `/LICENSE` for details of the license.
|
package/dist/api.d.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import Taro from '@tarojs/taro';
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
2
|
+
declare function navigateTo(option: Taro.navigateTo.Option): ReturnType<typeof Taro.navigateTo>;
|
|
3
|
+
declare function redirectTo(option: Taro.redirectTo.Option): ReturnType<typeof Taro.redirectTo>;
|
|
4
|
+
declare function navigateBack(option?: Taro.navigateBack.Option): ReturnType<typeof Taro.navigateBack>;
|
|
5
|
+
declare function switchTab(option: Taro.switchTab.Option): ReturnType<typeof Taro.switchTab>;
|
|
6
|
+
declare function reLaunch(option: Taro.reLaunch.Option): ReturnType<typeof Taro.reLaunch>;
|
|
7
|
+
declare function getCurrentPages(): Taro.Page[];
|
|
8
|
+
export { navigateTo, redirectTo, navigateBack, switchTab, reLaunch, getCurrentPages };
|
package/dist/api.js
CHANGED
|
@@ -16,14 +16,13 @@ function processNavigateUrl(option) {
|
|
|
16
16
|
const parts = routesAlias.getOrigin(history.location.pathname).split('/');
|
|
17
17
|
parts.pop();
|
|
18
18
|
pathPieces.pathname.split('/').forEach((item) => {
|
|
19
|
-
if (item === '.')
|
|
19
|
+
if (item === '.') {
|
|
20
20
|
return;
|
|
21
|
+
}
|
|
21
22
|
item === '..' ? parts.pop() : parts.push(item);
|
|
22
23
|
});
|
|
23
24
|
pathPieces.pathname = parts.join('/');
|
|
24
25
|
}
|
|
25
|
-
// 确保是 / 开头的路径
|
|
26
|
-
pathPieces.pathname = addLeadingSlash(pathPieces.pathname);
|
|
27
26
|
// 处理自定义路由
|
|
28
27
|
pathPieces.pathname = routesAlias.getAlias(addLeadingSlash(pathPieces.pathname));
|
|
29
28
|
// 处理 basename
|
|
@@ -53,8 +52,21 @@ function navigate(option, method) {
|
|
|
53
52
|
if ('url' in option) {
|
|
54
53
|
const pathPieces = processNavigateUrl(option);
|
|
55
54
|
const state = { timestamp: Date.now() };
|
|
55
|
+
if (pathPieces.pathname) {
|
|
56
|
+
const originPath = routesAlias.getOrigin(pathPieces.pathname);
|
|
57
|
+
if (!RouterConfig.isPage(addLeadingSlash(originPath)) && !RouterConfig.isPage(addLeadingSlash(pathPieces.pathname))) {
|
|
58
|
+
const res = { errMsg: `${method}:fail page ${originPath} is not found` };
|
|
59
|
+
fail === null || fail === void 0 ? void 0 : fail(res);
|
|
60
|
+
complete === null || complete === void 0 ? void 0 : complete(res);
|
|
61
|
+
if (fail || complete) {
|
|
62
|
+
return resolve(res);
|
|
63
|
+
}
|
|
64
|
+
else {
|
|
65
|
+
return reject(res);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
}
|
|
56
69
|
if (method === 'navigateTo') {
|
|
57
|
-
// Note: 由于 spa 会针对弱网情况下,短时间内多次跳转同一个页面跳转加了锁,后续如果有用户反馈返回无效,那可能是这个问题
|
|
58
70
|
history.push(pathPieces, state);
|
|
59
71
|
}
|
|
60
72
|
else if (method === 'redirectTo' || method === 'switchTab') {
|
package/dist/events/resize.d.ts
CHANGED
|
@@ -1,2 +1,3 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
1
|
+
import { PageInstance } from '@tarojs/runtime';
|
|
2
|
+
declare function bindPageResize(page: PageInstance): void;
|
|
3
|
+
export { bindPageResize };
|
package/dist/events/scroll.d.ts
CHANGED
|
@@ -1,2 +1,3 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
1
|
+
import { PageInstance } from '@tarojs/runtime';
|
|
2
|
+
declare function bindPageScroll(page: PageInstance, scrollEl: HTMLElement | Window, distance?: number): void;
|
|
3
|
+
export { bindPageScroll };
|
package/dist/history.d.ts
CHANGED
|
@@ -1,8 +1,6 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import
|
|
3
|
-
|
|
4
|
-
import type { StateEvent } from '../types/history';
|
|
5
|
-
export declare let history: History;
|
|
1
|
+
import { IH5RouterConfig } from '@tarojs/taro/types/compile';
|
|
2
|
+
import { StateEvent } from '../types/history';
|
|
3
|
+
declare let history: History;
|
|
6
4
|
declare class MpaHistory implements History {
|
|
7
5
|
action: Action;
|
|
8
6
|
get location(): Location;
|
|
@@ -19,8 +17,8 @@ declare class MpaHistory implements History {
|
|
|
19
17
|
replaceState: globalThis.History['replaceState'];
|
|
20
18
|
eventState(action: Required<StateEvent>['action']): (data: any, unused: string, url?: string | URL | null) => any;
|
|
21
19
|
}
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
export
|
|
20
|
+
declare function setHistory(h: History, base?: string): void;
|
|
21
|
+
declare function createMpaHistory(_?: HashHistoryOptions | BrowserHistoryOptions): MpaHistory;
|
|
22
|
+
declare function setHistoryMode(mode?: IH5RouterConfig['mode'], base?: string): void;
|
|
23
|
+
declare function prependBasename(url?: string): string;
|
|
24
|
+
export { history, setHistory, createMpaHistory, createBrowserHistory, createHashHistory, setHistoryMode, prependBasename };
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { Action, createBrowserHistory, createHashHistory, Blocker, BrowserHistoryOptions, HashHistoryOptions, History, Listener, Location, Path, To } from "history";
|
|
2
|
+
import { IH5RouterConfig } from "@tarojs/taro/types/compile";
|
|
3
|
+
import { StateEvent } from "../types/history";
|
|
4
|
+
import { MpaRouterConfig, SpaRouterConfig } from '../types/router';
|
|
5
|
+
import Taro from "@tarojs/taro";
|
|
6
|
+
import { AppInstance } from "@tarojs/runtime";
|
|
7
|
+
import { MpaRouterConfig as MpaRouterConfig$0 } from "../../types/router";
|
|
8
|
+
import { SpaRouterConfig as SpaRouterConfig$0 } from "../../types/router";
|
|
9
|
+
declare let history: History;
|
|
10
|
+
declare class MpaHistory implements History {
|
|
11
|
+
action: Action;
|
|
12
|
+
get location(): Location;
|
|
13
|
+
createHref(_to: To): string;
|
|
14
|
+
parseUrl(to: Partial<Path>): string;
|
|
15
|
+
push(to: Partial<Path>, _state?: Record<string, unknown>): void;
|
|
16
|
+
replace(to: Partial<Path>, _state?: Record<string, unknown>): void;
|
|
17
|
+
go(delta: number): void;
|
|
18
|
+
back: () => void;
|
|
19
|
+
forward: () => void;
|
|
20
|
+
listen(listener: Listener): () => void;
|
|
21
|
+
block(_blocker: Blocker): () => void;
|
|
22
|
+
pushState: globalThis.History["pushState"];
|
|
23
|
+
replaceState: globalThis.History["replaceState"];
|
|
24
|
+
eventState(action: Required<StateEvent>["action"]): (data: any, unused: string, url?: string | URL | null) => any;
|
|
25
|
+
}
|
|
26
|
+
declare function setHistory(h: History, base?: string): void;
|
|
27
|
+
declare function createMpaHistory(_?: HashHistoryOptions | BrowserHistoryOptions): MpaHistory;
|
|
28
|
+
declare function setHistoryMode(mode?: IH5RouterConfig["mode"], base?: string): void;
|
|
29
|
+
declare function prependBasename(url?: string): string;
|
|
30
|
+
declare function navigateTo(option: Taro.navigateTo.Option): ReturnType<typeof Taro.navigateTo>;
|
|
31
|
+
declare function redirectTo(option: Taro.redirectTo.Option): ReturnType<typeof Taro.redirectTo>;
|
|
32
|
+
declare function navigateBack(option?: Taro.navigateBack.Option): ReturnType<typeof Taro.navigateBack>;
|
|
33
|
+
declare function switchTab(option: Taro.switchTab.Option): ReturnType<typeof Taro.switchTab>;
|
|
34
|
+
declare function reLaunch(option: Taro.reLaunch.Option): ReturnType<typeof Taro.reLaunch>;
|
|
35
|
+
declare function getCurrentPages(): Taro.Page[];
|
|
36
|
+
// TODO 支持多路由 (APP 生命周期仅触发一次)
|
|
37
|
+
/** Note: 关于多页面应用
|
|
38
|
+
* - 需要配置路由映射(根目录跳转、404 页面……)
|
|
39
|
+
* - app.onPageNotFound 事件不支持
|
|
40
|
+
* - 应用生命周期可能多次触发
|
|
41
|
+
* - TabBar 会多次加载
|
|
42
|
+
* - 不支持路由动画
|
|
43
|
+
*/
|
|
44
|
+
declare function createMultiRouter(history: History, app: AppInstance, config: MpaRouterConfig$0, framework?: string): Promise<void>;
|
|
45
|
+
declare function createRouter(history: History, app: AppInstance, config: SpaRouterConfig$0, framework?: string): () => void;
|
|
46
|
+
declare class RoutesAlias {
|
|
47
|
+
conf: Array<string[]>;
|
|
48
|
+
set(customRoutes?: Record<string, string | string[]>): void;
|
|
49
|
+
getConfig: (url?: string) => string[];
|
|
50
|
+
getOrigin: (url?: string) => string;
|
|
51
|
+
getAlias: (url?: string) => string;
|
|
52
|
+
getAll: (url?: string) => string[];
|
|
53
|
+
}
|
|
54
|
+
declare const routesAlias: RoutesAlias;
|
|
55
|
+
declare const isWeixin: () => boolean;
|
|
56
|
+
declare const isDingTalk: () => boolean;
|
|
57
|
+
declare function setMpaTitle(title: string): void;
|
|
58
|
+
declare function setTitle(title: string): void;
|
|
59
|
+
declare function setNavigationBarStyle(option: {
|
|
60
|
+
backgroundColor: string;
|
|
61
|
+
frontColor: string;
|
|
62
|
+
}): void;
|
|
63
|
+
declare function setNavigationBarLoading(loading: boolean): void;
|
|
64
|
+
declare function handleAppMount(config: SpaRouterConfig | MpaRouterConfig, _: History, appId?: string): void;
|
|
65
|
+
declare function handleAppMountWithTabbar(config: SpaRouterConfig | MpaRouterConfig, history: History, appId?: string): void;
|
|
66
|
+
export { navigateTo, redirectTo, navigateBack, switchTab, reLaunch, getCurrentPages, history, setHistory, createMpaHistory, createBrowserHistory, createHashHistory, setHistoryMode, prependBasename, createMultiRouter, createRouter, routesAlias, isWeixin, isDingTalk, setMpaTitle, setTitle, setNavigationBarStyle, setNavigationBarLoading, handleAppMount, handleAppMountWithTabbar };
|
package/dist/index.cjs.js
CHANGED
|
@@ -15,8 +15,7 @@ var UniversalRouter = require('universal-router');
|
|
|
15
15
|
function loadAnimateStyle(ms = 300) {
|
|
16
16
|
const css = `
|
|
17
17
|
body {
|
|
18
|
-
|
|
19
|
-
overflow: hidden;
|
|
18
|
+
overflow: hidden; // 防止 iOS 页面滚动
|
|
20
19
|
}
|
|
21
20
|
.taro_router > .taro_page {
|
|
22
21
|
position: absolute;
|
|
@@ -73,9 +72,8 @@ ${enableTabBar ? `
|
|
|
73
72
|
}
|
|
74
73
|
|
|
75
74
|
` : ''}
|
|
76
|
-
.taro_page_shade
|
|
77
|
-
.taro_page_shade.taro_tabbar_page
|
|
78
|
-
.taro_router > .taro_page.taro_page_show.taro_page_stationed:not(.taro_page_shade):not(.taro_tabbar_page):not(:last-child):has(+.taro_page_stationed) {
|
|
75
|
+
.taro_page_shade,
|
|
76
|
+
.taro_router > .taro_page.taro_page_show.taro_page_stationed:not(.taro_page_shade):not(.taro_tabbar_page):not(:last-child) {
|
|
79
77
|
display: none;
|
|
80
78
|
}
|
|
81
79
|
`;
|
|
@@ -143,7 +141,7 @@ function loadNavigationBarStyle() {
|
|
|
143
141
|
to {
|
|
144
142
|
transform: rotate(360deg);
|
|
145
143
|
}
|
|
146
|
-
}
|
|
144
|
+
}
|
|
147
145
|
|
|
148
146
|
.taro-navigation-bar-no-icon > .taro-navigation-bar-home {
|
|
149
147
|
display: none;
|
|
@@ -265,9 +263,8 @@ class RouterConfig {
|
|
|
265
263
|
return this.router.mode || 'hash';
|
|
266
264
|
}
|
|
267
265
|
static get customRoutes() { return this.router.customRoutes || {}; }
|
|
268
|
-
// 这个方法不考虑 basename 和 customRoutes,只判断原始的 url 是否在 pages 中
|
|
269
266
|
static isPage(url = '') {
|
|
270
|
-
return this.pages.findIndex(e =>
|
|
267
|
+
return this.pages.findIndex(e => prependBasename(e) === url) !== -1;
|
|
271
268
|
}
|
|
272
269
|
}
|
|
273
270
|
|
|
@@ -536,14 +533,13 @@ function processNavigateUrl(option) {
|
|
|
536
533
|
const parts = routesAlias.getOrigin(exports.history.location.pathname).split('/');
|
|
537
534
|
parts.pop();
|
|
538
535
|
pathPieces.pathname.split('/').forEach((item) => {
|
|
539
|
-
if (item === '.')
|
|
536
|
+
if (item === '.') {
|
|
540
537
|
return;
|
|
538
|
+
}
|
|
541
539
|
item === '..' ? parts.pop() : parts.push(item);
|
|
542
540
|
});
|
|
543
541
|
pathPieces.pathname = parts.join('/');
|
|
544
542
|
}
|
|
545
|
-
// 确保是 / 开头的路径
|
|
546
|
-
pathPieces.pathname = runtime.addLeadingSlash(pathPieces.pathname);
|
|
547
543
|
// 处理自定义路由
|
|
548
544
|
pathPieces.pathname = routesAlias.getAlias(runtime.addLeadingSlash(pathPieces.pathname));
|
|
549
545
|
// 处理 basename
|
|
@@ -573,8 +569,21 @@ function navigate(option, method) {
|
|
|
573
569
|
if ('url' in option) {
|
|
574
570
|
const pathPieces = processNavigateUrl(option);
|
|
575
571
|
const state = { timestamp: Date.now() };
|
|
572
|
+
if (pathPieces.pathname) {
|
|
573
|
+
const originPath = routesAlias.getOrigin(pathPieces.pathname);
|
|
574
|
+
if (!RouterConfig.isPage(runtime.addLeadingSlash(originPath)) && !RouterConfig.isPage(runtime.addLeadingSlash(pathPieces.pathname))) {
|
|
575
|
+
const res = { errMsg: `${method}:fail page ${originPath} is not found` };
|
|
576
|
+
fail === null || fail === void 0 ? void 0 : fail(res);
|
|
577
|
+
complete === null || complete === void 0 ? void 0 : complete(res);
|
|
578
|
+
if (fail || complete) {
|
|
579
|
+
return resolve(res);
|
|
580
|
+
}
|
|
581
|
+
else {
|
|
582
|
+
return reject(res);
|
|
583
|
+
}
|
|
584
|
+
}
|
|
585
|
+
}
|
|
576
586
|
if (method === 'navigateTo') {
|
|
577
|
-
// Note: 由于 spa 会针对弱网情况下,短时间内多次跳转同一个页面跳转加了锁,后续如果有用户反馈返回无效,那可能是这个问题
|
|
578
587
|
exports.history.push(pathPieces, state);
|
|
579
588
|
}
|
|
580
589
|
else if (method === 'redirectTo' || method === 'switchTab') {
|
|
@@ -844,8 +853,8 @@ const launchStampId$1 = createStampId$1();
|
|
|
844
853
|
* - 不支持路由动画
|
|
845
854
|
*/
|
|
846
855
|
function createMultiRouter(history, app, config, framework) {
|
|
856
|
+
var _a, _b, _c, _d, _e, _f;
|
|
847
857
|
return tslib.__awaiter(this, void 0, void 0, function* () {
|
|
848
|
-
var _a, _b, _c, _d, _e, _f;
|
|
849
858
|
if (typeof app.onUnhandledRejection === 'function') {
|
|
850
859
|
window.addEventListener('unhandledrejection', app.onUnhandledRejection);
|
|
851
860
|
}
|
|
@@ -853,7 +862,7 @@ function createMultiRouter(history, app, config, framework) {
|
|
|
853
862
|
RouterConfig.config = config;
|
|
854
863
|
const handler = new MultiPageHandler(config, history);
|
|
855
864
|
const launchParam = {
|
|
856
|
-
path: config.pageName,
|
|
865
|
+
path: config.pageName,
|
|
857
866
|
query: handler.getQuery(launchStampId$1),
|
|
858
867
|
scene: 0,
|
|
859
868
|
shareTicket: '',
|
|
@@ -932,7 +941,7 @@ class NavigationBarHandler {
|
|
|
932
941
|
});
|
|
933
942
|
}
|
|
934
943
|
toHomeFn() {
|
|
935
|
-
reLaunch({ url: this.pageContext.
|
|
944
|
+
reLaunch({ url: this.pageContext.homePage });
|
|
936
945
|
}
|
|
937
946
|
backFn() {
|
|
938
947
|
navigateBack();
|
|
@@ -981,7 +990,7 @@ class NavigationBarHandler {
|
|
|
981
990
|
this.setNavigationLoading();
|
|
982
991
|
}
|
|
983
992
|
setCacheValue() {
|
|
984
|
-
const currentPage = this.pageContext.
|
|
993
|
+
const currentPage = this.pageContext.currentPage;
|
|
985
994
|
if (typeof this.cache[currentPage] !== 'object') {
|
|
986
995
|
this.cache[currentPage] = {};
|
|
987
996
|
}
|
|
@@ -1012,7 +1021,7 @@ class NavigationBarHandler {
|
|
|
1012
1021
|
var _a;
|
|
1013
1022
|
if (!this.navigationBarElement)
|
|
1014
1023
|
return;
|
|
1015
|
-
const currentPage = this.pageContext.
|
|
1024
|
+
const currentPage = this.pageContext.currentPage;
|
|
1016
1025
|
let isShow;
|
|
1017
1026
|
if (typeof show === 'boolean') {
|
|
1018
1027
|
isShow = show;
|
|
@@ -1037,7 +1046,7 @@ class NavigationBarHandler {
|
|
|
1037
1046
|
var _a, _b, _c;
|
|
1038
1047
|
if (!this.navigationBarElement)
|
|
1039
1048
|
return;
|
|
1040
|
-
const currentPage = this.pageContext.
|
|
1049
|
+
const currentPage = this.pageContext.currentPage;
|
|
1041
1050
|
let color;
|
|
1042
1051
|
if (typeof backgroundColor === 'string') {
|
|
1043
1052
|
color = backgroundColor;
|
|
@@ -1061,7 +1070,7 @@ class NavigationBarHandler {
|
|
|
1061
1070
|
var _a, _b, _c;
|
|
1062
1071
|
if (!this.navigationBarElement)
|
|
1063
1072
|
return;
|
|
1064
|
-
const currentPage = this.pageContext.
|
|
1073
|
+
const currentPage = this.pageContext.currentPage;
|
|
1065
1074
|
let color;
|
|
1066
1075
|
if (typeof fontColor === 'string') {
|
|
1067
1076
|
color = fontColor;
|
|
@@ -1083,7 +1092,7 @@ class NavigationBarHandler {
|
|
|
1083
1092
|
}
|
|
1084
1093
|
setTitle(title) {
|
|
1085
1094
|
var _a, _b, _c;
|
|
1086
|
-
const currentPage = this.pageContext.
|
|
1095
|
+
const currentPage = this.pageContext.currentPage;
|
|
1087
1096
|
let proceedTitle;
|
|
1088
1097
|
if (typeof title === 'string') {
|
|
1089
1098
|
proceedTitle = title;
|
|
@@ -1164,7 +1173,6 @@ class PageHandler {
|
|
|
1164
1173
|
this.defaultAnimation = { duration: 300, delay: 50 };
|
|
1165
1174
|
this.config = config;
|
|
1166
1175
|
this.homePage = runtime.getHomePage(this.routes[0].path, this.basename, this.customRoutes, this.config.entryPagePath);
|
|
1167
|
-
this.originHomePage = this.config.entryPagePath || this.routes[0].path || this.basename;
|
|
1168
1176
|
this.mount();
|
|
1169
1177
|
this.navigationBarHandler = new NavigationBarHandler(this);
|
|
1170
1178
|
}
|
|
@@ -1198,14 +1206,14 @@ class PageHandler {
|
|
|
1198
1206
|
}
|
|
1199
1207
|
set pathname(p) { this.router.pathname = p; }
|
|
1200
1208
|
get pathname() { return this.router.pathname; }
|
|
1201
|
-
// Note: 把 pathname 转换为原始路径,主要是处理 customRoutes 和 basename
|
|
1202
|
-
get originPathname() { return routesAlias.getOrigin(runtime.addLeadingSlash(runtime.stripBasename(this.pathname, this.basename))); }
|
|
1203
1209
|
get basename() { return this.router.basename || ''; }
|
|
1204
1210
|
get pageConfig() {
|
|
1211
|
+
const routePath = runtime.addLeadingSlash(runtime.stripBasename(this.pathname, this.basename));
|
|
1205
1212
|
const homePage = runtime.addLeadingSlash(this.homePage);
|
|
1206
1213
|
return this.routes.find(r => {
|
|
1214
|
+
var _a;
|
|
1207
1215
|
const pagePath = runtime.addLeadingSlash(r.path);
|
|
1208
|
-
return [pagePath, homePage].includes(
|
|
1216
|
+
return [pagePath, homePage].includes(routePath) || ((_a = routesAlias.getConfig(pagePath)) === null || _a === void 0 ? void 0 : _a.includes(routePath));
|
|
1209
1217
|
});
|
|
1210
1218
|
}
|
|
1211
1219
|
isTabBar(pathname) {
|
|
@@ -1395,35 +1403,24 @@ class PageHandler {
|
|
|
1395
1403
|
});
|
|
1396
1404
|
}
|
|
1397
1405
|
}
|
|
1398
|
-
hide(page
|
|
1399
|
-
var _a
|
|
1406
|
+
hide(page) {
|
|
1407
|
+
var _a;
|
|
1400
1408
|
if (!page)
|
|
1401
1409
|
return;
|
|
1402
1410
|
// NOTE: 修复多页并发问题,此处可能因为路由跳转过快,执行时页面可能还没有创建成功
|
|
1403
1411
|
const pageEl = this.getPageContainer(page);
|
|
1404
1412
|
if (pageEl) {
|
|
1405
|
-
if (
|
|
1406
|
-
|
|
1407
|
-
|
|
1408
|
-
this.hideTimer = null;
|
|
1409
|
-
(_c = (_b = (_a = this.lastHidePage) === null || _a === void 0 ? void 0 : _a.classList) === null || _b === void 0 ? void 0 : _b.add) === null || _c === void 0 ? void 0 : _c.call(_b, 'taro_page_shade');
|
|
1410
|
-
}
|
|
1411
|
-
this.lastHidePage = pageEl;
|
|
1412
|
-
this.hideTimer = setTimeout(() => {
|
|
1413
|
-
this.hideTimer = null;
|
|
1414
|
-
pageEl.classList.add('taro_page_shade');
|
|
1415
|
-
}, this.animationDuration + this.animationDelay);
|
|
1416
|
-
(_d = page.onHide) === null || _d === void 0 ? void 0 : _d.call(page);
|
|
1417
|
-
}
|
|
1418
|
-
else {
|
|
1419
|
-
if (this.hideTimer) {
|
|
1420
|
-
clearTimeout(this.hideTimer);
|
|
1421
|
-
this.hideTimer = null;
|
|
1422
|
-
(_g = (_f = (_e = this.lastHidePage) === null || _e === void 0 ? void 0 : _e.classList) === null || _f === void 0 ? void 0 : _f.add) === null || _g === void 0 ? void 0 : _g.call(_f, 'taro_page_shade');
|
|
1423
|
-
}
|
|
1413
|
+
if (this.hideTimer) {
|
|
1414
|
+
clearTimeout(this.hideTimer);
|
|
1415
|
+
this.hideTimer = null;
|
|
1424
1416
|
pageEl.classList.add('taro_page_shade');
|
|
1425
|
-
this.lastHidePage = pageEl;
|
|
1426
1417
|
}
|
|
1418
|
+
this.lastHidePage = pageEl;
|
|
1419
|
+
this.hideTimer = setTimeout(() => {
|
|
1420
|
+
this.hideTimer = null;
|
|
1421
|
+
pageEl.classList.add('taro_page_shade');
|
|
1422
|
+
}, this.animationDuration + this.animationDelay);
|
|
1423
|
+
(_a = page.onHide) === null || _a === void 0 ? void 0 : _a.call(page);
|
|
1427
1424
|
}
|
|
1428
1425
|
else {
|
|
1429
1426
|
setTimeout(() => this.hide(page), 0);
|
|
@@ -1494,10 +1491,6 @@ function createRouter(history$1, app, config, framework) {
|
|
|
1494
1491
|
}
|
|
1495
1492
|
RouterConfig.config = config;
|
|
1496
1493
|
const handler = new PageHandler(config, history$1);
|
|
1497
|
-
// Note: 弱网情况下,快速切换 tab,会造成同个页面实例被多次挂在到页面上,原因是资源请求是异步的,短时间内发起多个请求,
|
|
1498
|
-
// 会在资源加载完成后,同时走到挂载的逻辑,造成 pageStampId 更新不及时,两个 page 的Id 相同,后面很多操作是通过 getElementById 来进行的
|
|
1499
|
-
// 所以需要加一个锁来应对这个情况
|
|
1500
|
-
const pageLock = {};
|
|
1501
1494
|
routesAlias.set(handler.router.customRoutes);
|
|
1502
1495
|
const basename = handler.router.basename;
|
|
1503
1496
|
const routes = handler.routes.map(route => {
|
|
@@ -1519,35 +1512,29 @@ function createRouter(history$1, app, config, framework) {
|
|
|
1519
1512
|
runtime.eventCenter.trigger('__taroRouterLaunch', launchParam);
|
|
1520
1513
|
(_a = app.onLaunch) === null || _a === void 0 ? void 0 : _a.call(app, launchParam);
|
|
1521
1514
|
app.onError && window.addEventListener('error', e => { var _a; return (_a = app.onError) === null || _a === void 0 ? void 0 : _a.call(app, e.message); });
|
|
1522
|
-
const render = (
|
|
1523
|
-
var _d, _e, _f, _g, _h, _j, _k
|
|
1524
|
-
|
|
1525
|
-
|
|
1526
|
-
if ((_d = window.__taroAppConfig) === null || _d === void 0 ? void 0 : _d.usingWindowScroll)
|
|
1515
|
+
const render = ({ location, action }) => tslib.__awaiter(this, void 0, void 0, function* () {
|
|
1516
|
+
var _c, _d, _e, _f, _g, _h, _j, _k;
|
|
1517
|
+
handler.pathname = decodeURI(location.pathname);
|
|
1518
|
+
if ((_c = window.__taroAppConfig) === null || _c === void 0 ? void 0 : _c.usingWindowScroll)
|
|
1527
1519
|
window.scrollTo(0, 0);
|
|
1528
1520
|
runtime.eventCenter.trigger('__taroRouterChange', {
|
|
1529
1521
|
toLocation: {
|
|
1530
|
-
path:
|
|
1522
|
+
path: handler.pathname
|
|
1531
1523
|
}
|
|
1532
1524
|
});
|
|
1533
|
-
let element,
|
|
1534
|
-
const routerPath = handler.router.forcePath || currentPathname;
|
|
1535
|
-
pageLock[routerPath] = typeof pageLock[routerPath] === 'number' ? pageLock[routerPath] + 1 : 1;
|
|
1536
|
-
const currentLock = pageLock[routerPath];
|
|
1537
|
-
let postLock;
|
|
1525
|
+
let element, params;
|
|
1538
1526
|
try {
|
|
1539
|
-
const result = yield router.resolve(
|
|
1540
|
-
[element,
|
|
1541
|
-
postLock = pageLock[routerPath];
|
|
1527
|
+
const result = yield router.resolve(handler.router.forcePath || handler.pathname);
|
|
1528
|
+
[element, , params] = yield Promise.all(result);
|
|
1542
1529
|
}
|
|
1543
1530
|
catch (error) {
|
|
1544
1531
|
if (error.status === 404) {
|
|
1545
1532
|
const notFoundEvent = {
|
|
1546
1533
|
isEntryPage: stacks.length === 0,
|
|
1547
|
-
path:
|
|
1534
|
+
path: handler.pathname,
|
|
1548
1535
|
query: handler.getQuery(createStampId()),
|
|
1549
1536
|
};
|
|
1550
|
-
(
|
|
1537
|
+
(_d = app.onPageNotFound) === null || _d === void 0 ? void 0 : _d.call(app, notFoundEvent);
|
|
1551
1538
|
runtime.eventCenter.trigger('__taroRouterNotFound', notFoundEvent);
|
|
1552
1539
|
}
|
|
1553
1540
|
else if (/Loading hot update .* failed./.test(error.message)) {
|
|
@@ -1558,16 +1545,13 @@ function createRouter(history$1, app, config, framework) {
|
|
|
1558
1545
|
throw error;
|
|
1559
1546
|
}
|
|
1560
1547
|
}
|
|
1561
|
-
if (!element
|
|
1548
|
+
if (!element)
|
|
1562
1549
|
return;
|
|
1563
|
-
|
|
1564
|
-
|
|
1565
|
-
|
|
1566
|
-
|
|
1567
|
-
let
|
|
1568
|
-
let navigationStyle = ((_g = config === null || config === void 0 ? void 0 : config.window) === null || _g === void 0 ? void 0 : _g.navigationStyle) || 'default';
|
|
1569
|
-
let navigationBarTextStyle = ((_h = config === null || config === void 0 ? void 0 : config.window) === null || _h === void 0 ? void 0 : _h.navigationBarTextStyle) || 'white';
|
|
1570
|
-
let navigationBarBackgroundColor = ((_j = config === null || config === void 0 ? void 0 : config.window) === null || _j === void 0 ? void 0 : _j.navigationBarBackgroundColor) || '#000000';
|
|
1550
|
+
const pageConfig = handler.pageConfig;
|
|
1551
|
+
let enablePullDownRefresh = ((_e = config === null || config === void 0 ? void 0 : config.window) === null || _e === void 0 ? void 0 : _e.enablePullDownRefresh) || false;
|
|
1552
|
+
let navigationStyle = ((_f = config === null || config === void 0 ? void 0 : config.window) === null || _f === void 0 ? void 0 : _f.navigationStyle) || 'default';
|
|
1553
|
+
let navigationBarTextStyle = ((_g = config === null || config === void 0 ? void 0 : config.window) === null || _g === void 0 ? void 0 : _g.navigationBarTextStyle) || 'white';
|
|
1554
|
+
let navigationBarBackgroundColor = ((_h = config === null || config === void 0 ? void 0 : config.window) === null || _h === void 0 ? void 0 : _h.navigationBarBackgroundColor) || '#000000';
|
|
1571
1555
|
if (pageConfig) {
|
|
1572
1556
|
if (typeof pageConfig.enablePullDownRefresh === 'boolean') {
|
|
1573
1557
|
enablePullDownRefresh = pageConfig.enablePullDownRefresh;
|
|
@@ -1584,7 +1568,8 @@ function createRouter(history$1, app, config, framework) {
|
|
|
1584
1568
|
}
|
|
1585
1569
|
runtime.eventCenter.trigger('__taroSetNavigationStyle', navigationStyle, navigationBarTextStyle, navigationBarBackgroundColor);
|
|
1586
1570
|
const currentPage = runtime.Current.page;
|
|
1587
|
-
const
|
|
1571
|
+
const pathname = handler.pathname;
|
|
1572
|
+
const methodName = (_j = stacks.method) !== null && _j !== void 0 ? _j : '';
|
|
1588
1573
|
const cacheTabs = stacks.getTabs();
|
|
1589
1574
|
let shouldLoad = false;
|
|
1590
1575
|
stacks.method = '';
|
|
@@ -1599,11 +1584,10 @@ function createRouter(history$1, app, config, framework) {
|
|
|
1599
1584
|
}
|
|
1600
1585
|
shouldLoad = true;
|
|
1601
1586
|
}
|
|
1602
|
-
else if (currentPage && handler.isTabBar(pathname)) {
|
|
1587
|
+
else if (currentPage && handler.isTabBar(handler.pathname)) {
|
|
1603
1588
|
if (handler.isSamePage(currentPage))
|
|
1604
1589
|
return;
|
|
1605
1590
|
if (handler.isTabBar(currentPage.path)) {
|
|
1606
|
-
// NOTE: 从 tabBar 页面切换到 tabBar 页面
|
|
1607
1591
|
handler.hide(currentPage);
|
|
1608
1592
|
stacks.pushTab(currentPage.path.split('?')[0]);
|
|
1609
1593
|
}
|
|
@@ -1617,8 +1601,8 @@ function createRouter(history$1, app, config, framework) {
|
|
|
1617
1601
|
handler.unload(currentPage, stacks.length, true);
|
|
1618
1602
|
}
|
|
1619
1603
|
}
|
|
1620
|
-
if (cacheTabs[pathname]) {
|
|
1621
|
-
stacks.popTab(pathname);
|
|
1604
|
+
if (cacheTabs[handler.pathname]) {
|
|
1605
|
+
stacks.popTab(handler.pathname);
|
|
1622
1606
|
return handler.show(stacks.getItem(0), pageConfig, 0);
|
|
1623
1607
|
}
|
|
1624
1608
|
shouldLoad = true;
|
|
@@ -1647,11 +1631,11 @@ function createRouter(history$1, app, config, framework) {
|
|
|
1647
1631
|
shouldLoad = true;
|
|
1648
1632
|
}
|
|
1649
1633
|
else if (action === 'PUSH') {
|
|
1650
|
-
handler.hide(currentPage
|
|
1634
|
+
handler.hide(currentPage);
|
|
1651
1635
|
shouldLoad = true;
|
|
1652
1636
|
}
|
|
1653
1637
|
if (shouldLoad || stacks.length < 1) {
|
|
1654
|
-
const el = (
|
|
1638
|
+
const el = (_k = element.default) !== null && _k !== void 0 ? _k : element;
|
|
1655
1639
|
const loadConfig = Object.assign({}, pageConfig);
|
|
1656
1640
|
const stacksIndex = stacks.length;
|
|
1657
1641
|
delete loadConfig['path'];
|
|
@@ -1740,11 +1724,11 @@ function handleAppMountWithTabbar(config, history, appId = config.appId || 'app'
|
|
|
1740
1724
|
initNavigationBar(config, container);
|
|
1741
1725
|
}
|
|
1742
1726
|
|
|
1743
|
-
Object.defineProperty(exports,
|
|
1727
|
+
Object.defineProperty(exports, 'createBrowserHistory', {
|
|
1744
1728
|
enumerable: true,
|
|
1745
1729
|
get: function () { return history.createBrowserHistory; }
|
|
1746
1730
|
});
|
|
1747
|
-
Object.defineProperty(exports,
|
|
1731
|
+
Object.defineProperty(exports, 'createHashHistory', {
|
|
1748
1732
|
enumerable: true,
|
|
1749
1733
|
get: function () { return history.createHashHistory; }
|
|
1750
1734
|
});
|
package/dist/index.d.ts
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
export
|
|
6
|
-
export
|
|
7
|
-
export
|
|
8
|
-
export
|
|
9
|
-
export
|
|
1
|
+
import { History } from "./history.js";
|
|
2
|
+
import { MpaRouterConfig, SpaRouterConfig } from '../types/router';
|
|
3
|
+
declare function handleAppMount(config: SpaRouterConfig | MpaRouterConfig, _: History, appId?: string): void;
|
|
4
|
+
declare function handleAppMountWithTabbar(config: SpaRouterConfig | MpaRouterConfig, history: History, appId?: string): void;
|
|
5
|
+
export * from "./api.js";
|
|
6
|
+
export * from "./history.js";
|
|
7
|
+
export { createMultiRouter } from "./router/mpa.js";
|
|
8
|
+
export { createRouter } from "./router/spa.js";
|
|
9
|
+
export * from "./utils/index.js";
|
|
10
|
+
export { handleAppMount, handleAppMountWithTabbar };
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { Action, createBrowserHistory, createHashHistory, Blocker, BrowserHistoryOptions, HashHistoryOptions, History, Listener, Location, Path, To } from "history";
|
|
2
|
+
import { IH5RouterConfig } from "@tarojs/taro/types/compile";
|
|
3
|
+
import { StateEvent } from "../types/history";
|
|
4
|
+
import { MpaRouterConfig, SpaRouterConfig } from '../types/router';
|
|
5
|
+
import Taro from "@tarojs/taro";
|
|
6
|
+
import { AppInstance } from "@tarojs/runtime";
|
|
7
|
+
import { MpaRouterConfig as MpaRouterConfig$0 } from "../../types/router";
|
|
8
|
+
import { SpaRouterConfig as SpaRouterConfig$0 } from "../../types/router";
|
|
9
|
+
declare let history: History;
|
|
10
|
+
declare class MpaHistory implements History {
|
|
11
|
+
action: Action;
|
|
12
|
+
get location(): Location;
|
|
13
|
+
createHref(_to: To): string;
|
|
14
|
+
parseUrl(to: Partial<Path>): string;
|
|
15
|
+
push(to: Partial<Path>, _state?: Record<string, unknown>): void;
|
|
16
|
+
replace(to: Partial<Path>, _state?: Record<string, unknown>): void;
|
|
17
|
+
go(delta: number): void;
|
|
18
|
+
back: () => void;
|
|
19
|
+
forward: () => void;
|
|
20
|
+
listen(listener: Listener): () => void;
|
|
21
|
+
block(_blocker: Blocker): () => void;
|
|
22
|
+
pushState: globalThis.History["pushState"];
|
|
23
|
+
replaceState: globalThis.History["replaceState"];
|
|
24
|
+
eventState(action: Required<StateEvent>["action"]): (data: any, unused: string, url?: string | URL | null) => any;
|
|
25
|
+
}
|
|
26
|
+
declare function setHistory(h: History, base?: string): void;
|
|
27
|
+
declare function createMpaHistory(_?: HashHistoryOptions | BrowserHistoryOptions): MpaHistory;
|
|
28
|
+
declare function setHistoryMode(mode?: IH5RouterConfig["mode"], base?: string): void;
|
|
29
|
+
declare function prependBasename(url?: string): string;
|
|
30
|
+
declare function navigateTo(option: Taro.navigateTo.Option): ReturnType<typeof Taro.navigateTo>;
|
|
31
|
+
declare function redirectTo(option: Taro.redirectTo.Option): ReturnType<typeof Taro.redirectTo>;
|
|
32
|
+
declare function navigateBack(option?: Taro.navigateBack.Option): ReturnType<typeof Taro.navigateBack>;
|
|
33
|
+
declare function switchTab(option: Taro.switchTab.Option): ReturnType<typeof Taro.switchTab>;
|
|
34
|
+
declare function reLaunch(option: Taro.reLaunch.Option): ReturnType<typeof Taro.reLaunch>;
|
|
35
|
+
declare function getCurrentPages(): Taro.Page[];
|
|
36
|
+
// TODO 支持多路由 (APP 生命周期仅触发一次)
|
|
37
|
+
/** Note: 关于多页面应用
|
|
38
|
+
* - 需要配置路由映射(根目录跳转、404 页面……)
|
|
39
|
+
* - app.onPageNotFound 事件不支持
|
|
40
|
+
* - 应用生命周期可能多次触发
|
|
41
|
+
* - TabBar 会多次加载
|
|
42
|
+
* - 不支持路由动画
|
|
43
|
+
*/
|
|
44
|
+
declare function createMultiRouter(history: History, app: AppInstance, config: MpaRouterConfig$0, framework?: string): Promise<void>;
|
|
45
|
+
declare function createRouter(history: History, app: AppInstance, config: SpaRouterConfig$0, framework?: string): () => void;
|
|
46
|
+
declare class RoutesAlias {
|
|
47
|
+
conf: Array<string[]>;
|
|
48
|
+
set(customRoutes?: Record<string, string | string[]>): void;
|
|
49
|
+
getConfig: (url?: string) => string[];
|
|
50
|
+
getOrigin: (url?: string) => string;
|
|
51
|
+
getAlias: (url?: string) => string;
|
|
52
|
+
getAll: (url?: string) => string[];
|
|
53
|
+
}
|
|
54
|
+
declare const routesAlias: RoutesAlias;
|
|
55
|
+
declare const isWeixin: () => boolean;
|
|
56
|
+
declare const isDingTalk: () => boolean;
|
|
57
|
+
declare function setMpaTitle(title: string): void;
|
|
58
|
+
declare function setTitle(title: string): void;
|
|
59
|
+
declare function setNavigationBarStyle(option: {
|
|
60
|
+
backgroundColor: string;
|
|
61
|
+
frontColor: string;
|
|
62
|
+
}): void;
|
|
63
|
+
declare function setNavigationBarLoading(loading: boolean): void;
|
|
64
|
+
declare function handleAppMount(config: SpaRouterConfig | MpaRouterConfig, _: History, appId?: string): void;
|
|
65
|
+
declare function handleAppMountWithTabbar(config: SpaRouterConfig | MpaRouterConfig, history: History, appId?: string): void;
|
|
66
|
+
export { navigateTo, redirectTo, navigateBack, switchTab, reLaunch, getCurrentPages, history, setHistory, createMpaHistory, createBrowserHistory, createHashHistory, setHistoryMode, prependBasename, createMultiRouter, createRouter, routesAlias, isWeixin, isDingTalk, setMpaTitle, setTitle, setNavigationBarStyle, setNavigationBarLoading, handleAppMount, handleAppMountWithTabbar };
|