navigator的代码有边界问题

This commit is contained in:
Xu Chang 2025-11-12 12:56:28 +08:00
parent 5c710ef019
commit 3ca9d51d4d
10 changed files with 56 additions and 67 deletions

View File

@ -75,13 +75,12 @@ export class Navigator extends CommonNavigator {
wx.navigateTo({
url: url,
success: () => {
this.leave();
this.publish();
resolve(undefined);
},
fail: (err) => reject(err),
});
this.publish();
this.leave();
});
}
// 关闭当前页面跳转到应用内的某个页面但不允许跳转到tabBar页面。
@ -94,12 +93,12 @@ export class Navigator extends CommonNavigator {
wx.redirectTo({
url: url,
success: () => {
resolve(undefined);
this.leave();
this.publish();
resolve(undefined);
},
fail: (err) => reject(err),
});
this.leave();
});
}
//跳转到tabBar页面并关闭其他所有非tabBar页面用于跳转到主页。
@ -112,12 +111,12 @@ export class Navigator extends CommonNavigator {
wx.switchTab({
url: url,
success: () => {
resolve(undefined);
this.leave();
this.publish();
resolve(undefined);
},
fail: (err) => reject(err),
});
this.leave();
});
}
navigateBack(delta) {
@ -128,29 +127,23 @@ export class Navigator extends CommonNavigator {
wx.navigateBack({
delta: delta || 1,
success: () => {
resolve(undefined);
this.leave();
this.publish();
resolve(undefined);
},
fail: (err) => reject(err),
});
this.leave();
});
}
navigateBackOrRedirectTo(options, state, disableNamespace) {
if (!this.enter()) {
return;
}
const pages = getCurrentPages();
if (pages.length > 1) {
this.leave();
return this.navigateBack();
}
const isTabBar = options?.isTabBar;
if (isTabBar) {
this.leave();
return this.switchTab(options, state, disableNamespace);
}
this.leave();
return this.redirectTo(options, state, disableNamespace);
}
}

View File

@ -56,8 +56,8 @@ export class Navigator extends CommonNavigator {
const { url, props } = this.getUrlAndProps(options, state, disableNamespace);
const replaceAction = StackActions.replace(url, props);
this.history.dispatch(replaceAction);
this.publish();
this.leave();
this.publish();
}
async switchTab(options, state, disableNamespace) {
if (!this.enter()) {
@ -66,8 +66,8 @@ export class Navigator extends CommonNavigator {
const { url, props } = this.getUrlAndProps(options, state, disableNamespace);
const jumpToAction = TabActions.jumpTo(url, props);
this.history.dispatch(jumpToAction);
this.publish();
this.leave();
this.publish();
}
async navigateBack(delta) {
if (!this.enter()) {
@ -77,9 +77,12 @@ export class Navigator extends CommonNavigator {
if (canGoBack) {
const popAction = StackActions.pop(delta || 1);
this.history.dispatch(popAction);
this.leave();
this.publish();
}
this.leave();
else {
this.leave();
}
}
navigateBackOrRedirectTo(options, state, disableNamespace) {
if (!this.enter()) {
@ -88,13 +91,13 @@ export class Navigator extends CommonNavigator {
const canGoBack = this.history.canGoBack();
if (canGoBack) {
this.navigateBack();
this.publish();
this.leave();
this.publish();
return;
}
// 回最顶层
this.history.dispatch(StackActions.popToTop());
this.publish();
this.leave();
this.publish();
}
}

View File

@ -45,8 +45,8 @@ export class Navigator extends CommonNavigator {
}
const { url, props } = this.getUrlAndProps(options, state, disableNamespace);
this.history.push(url, props);
this.publish();
this.leave();
this.publish();
}
async redirectTo(options, state, disableNamespace) {
if (!this.enter()) {
@ -54,8 +54,8 @@ export class Navigator extends CommonNavigator {
}
const { url, props } = this.getUrlAndProps(options, state, disableNamespace);
this.history.replace(url, props);
this.publish();
this.leave();
this.publish();
}
async switchTab(options, state, disableNamespace) {
console.error('浏览器无switchTab');
@ -68,8 +68,8 @@ export class Navigator extends CommonNavigator {
return;
}
this.history.go(delta ? 0 - delta : -1);
this.publish();
this.leave();
this.publish();
}
navigateBackOrRedirectTo(options, state, disableNamespace) {
console.error('浏览器暂无法获得history堆栈');

View File

@ -78,13 +78,12 @@ class Navigator extends navigator_common_1.Navigator {
wx.navigateTo({
url: url,
success: () => {
this.leave();
this.publish();
resolve(undefined);
},
fail: (err) => reject(err),
});
this.publish();
this.leave();
});
}
// 关闭当前页面跳转到应用内的某个页面但不允许跳转到tabBar页面。
@ -97,12 +96,12 @@ class Navigator extends navigator_common_1.Navigator {
wx.redirectTo({
url: url,
success: () => {
resolve(undefined);
this.leave();
this.publish();
resolve(undefined);
},
fail: (err) => reject(err),
});
this.leave();
});
}
//跳转到tabBar页面并关闭其他所有非tabBar页面用于跳转到主页。
@ -115,12 +114,12 @@ class Navigator extends navigator_common_1.Navigator {
wx.switchTab({
url: url,
success: () => {
resolve(undefined);
this.leave();
this.publish();
resolve(undefined);
},
fail: (err) => reject(err),
});
this.leave();
});
}
navigateBack(delta) {
@ -131,29 +130,23 @@ class Navigator extends navigator_common_1.Navigator {
wx.navigateBack({
delta: delta || 1,
success: () => {
resolve(undefined);
this.leave();
this.publish();
resolve(undefined);
},
fail: (err) => reject(err),
});
this.leave();
});
}
navigateBackOrRedirectTo(options, state, disableNamespace) {
if (!this.enter()) {
return;
}
const pages = getCurrentPages();
if (pages.length > 1) {
this.leave();
return this.navigateBack();
}
const isTabBar = options?.isTabBar;
if (isTabBar) {
this.leave();
return this.switchTab(options, state, disableNamespace);
}
this.leave();
return this.redirectTo(options, state, disableNamespace);
}
}

View File

@ -59,8 +59,8 @@ class Navigator extends navigator_common_1.Navigator {
const { url, props } = this.getUrlAndProps(options, state, disableNamespace);
const replaceAction = native_1.StackActions.replace(url, props);
this.history.dispatch(replaceAction);
this.publish();
this.leave();
this.publish();
}
async switchTab(options, state, disableNamespace) {
if (!this.enter()) {
@ -69,8 +69,8 @@ class Navigator extends navigator_common_1.Navigator {
const { url, props } = this.getUrlAndProps(options, state, disableNamespace);
const jumpToAction = native_1.TabActions.jumpTo(url, props);
this.history.dispatch(jumpToAction);
this.publish();
this.leave();
this.publish();
}
async navigateBack(delta) {
if (!this.enter()) {
@ -80,9 +80,12 @@ class Navigator extends navigator_common_1.Navigator {
if (canGoBack) {
const popAction = native_1.StackActions.pop(delta || 1);
this.history.dispatch(popAction);
this.leave();
this.publish();
}
this.leave();
else {
this.leave();
}
}
navigateBackOrRedirectTo(options, state, disableNamespace) {
if (!this.enter()) {
@ -91,14 +94,14 @@ class Navigator extends navigator_common_1.Navigator {
const canGoBack = this.history.canGoBack();
if (canGoBack) {
this.navigateBack();
this.publish();
this.leave();
this.publish();
return;
}
// 回最顶层
this.history.dispatch(native_1.StackActions.popToTop());
this.publish();
this.leave();
this.publish();
}
}
exports.Navigator = Navigator;

View File

@ -48,8 +48,8 @@ class Navigator extends navigator_common_1.Navigator {
}
const { url, props } = this.getUrlAndProps(options, state, disableNamespace);
this.history.push(url, props);
this.publish();
this.leave();
this.publish();
}
async redirectTo(options, state, disableNamespace) {
if (!this.enter()) {
@ -57,8 +57,8 @@ class Navigator extends navigator_common_1.Navigator {
}
const { url, props } = this.getUrlAndProps(options, state, disableNamespace);
this.history.replace(url, props);
this.publish();
this.leave();
this.publish();
}
async switchTab(options, state, disableNamespace) {
console.error('浏览器无switchTab');
@ -71,8 +71,8 @@ class Navigator extends navigator_common_1.Navigator {
return;
}
this.history.go(delta ? 0 - delta : -1);
this.publish();
this.leave();
this.publish();
}
navigateBackOrRedirectTo(options, state, disableNamespace) {
console.error('浏览器暂无法获得history堆栈');

View File

@ -1,6 +1,6 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.createComponent = createComponent;
exports.createComponent = void 0;
/// <reference path="../node_modules/@types/wechat-miniprogram/index.d.ts" />
const assert_1 = require("oak-domain/lib/utils/assert");
const types_1 = require("oak-domain/lib/types");
@ -886,3 +886,4 @@ function createComponent(option, features) {
},
});
}
exports.createComponent = createComponent;

View File

@ -108,13 +108,12 @@ export class Navigator extends CommonNavigator {
wx.navigateTo({
url: url,
success: () => {
this.leave();
this.publish();
resolve(undefined);
},
fail: (err) => reject(err),
});
this.publish();
this.leave();
});
}
@ -133,12 +132,12 @@ export class Navigator extends CommonNavigator {
wx.redirectTo({
url: url,
success: () => {
resolve(undefined);
this.leave();
this.publish();
resolve(undefined);
},
fail: (err) => reject(err),
});
this.leave();
});
}
@ -156,12 +155,12 @@ export class Navigator extends CommonNavigator {
wx.switchTab({
url: url,
success: () => {
resolve(undefined);
this.leave();
this.publish();
resolve(undefined);
},
fail: (err) => reject(err),
});
this.leave();
});
}
@ -173,12 +172,12 @@ export class Navigator extends CommonNavigator {
wx.navigateBack({
delta: delta || 1,
success: () => {
resolve(undefined);
this.leave();
this.publish();
resolve(undefined);
},
fail: (err) => reject(err),
});
this.leave();
});
}
@ -193,20 +192,14 @@ export class Navigator extends CommonNavigator {
state?: Record<string, any>,
disableNamespace?: boolean
) {
if (!this.enter()) {
return;
}
const pages = getCurrentPages();
if (pages.length > 1) {
this.leave();
return this.navigateBack();
}
const isTabBar = options?.isTabBar;
if (isTabBar) {
this.leave();
return this.switchTab(options, state, disableNamespace);
}
this.leave();
return this.redirectTo(options, state, disableNamespace);
}
}

View File

@ -109,8 +109,8 @@ export class Navigator extends CommonNavigator {
);
const replaceAction = StackActions.replace(url, props);
this.history.dispatch(replaceAction);
this.publish();
this.leave();
this.publish();
}
async switchTab<
@ -131,8 +131,8 @@ export class Navigator extends CommonNavigator {
);
const jumpToAction = TabActions.jumpTo(url, props);
this.history.dispatch(jumpToAction);
this.publish();
this.leave();
this.publish();
}
async navigateBack(delta?: number) {
@ -143,9 +143,12 @@ export class Navigator extends CommonNavigator {
if (canGoBack) {
const popAction = StackActions.pop(delta || 1);
this.history.dispatch(popAction);
this.leave();
this.publish();
}
this.leave();
else {
this.leave();
}
}
navigateBackOrRedirectTo<
@ -165,13 +168,13 @@ export class Navigator extends CommonNavigator {
const canGoBack = this.history.canGoBack();
if (canGoBack) {
this.navigateBack();
this.publish();
this.leave();
this.publish();
return;
}
// 回最顶层
this.history.dispatch(StackActions.popToTop());
this.publish();
this.leave();
this.publish();
}
}

View File

@ -82,8 +82,8 @@ export class Navigator extends CommonNavigator {
disableNamespace
);
this.history.push(url, props);
this.publish();
this.leave();
this.publish();
}
async redirectTo<
@ -103,8 +103,8 @@ export class Navigator extends CommonNavigator {
disableNamespace
);
this.history.replace(url, props);
this.publish();
this.leave();
this.publish();
}
async switchTab<
@ -130,8 +130,8 @@ export class Navigator extends CommonNavigator {
return;
}
this.history.go(delta ? 0 - delta : -1);
this.publish();
this.leave();
this.publish();
}
navigateBackOrRedirectTo<