用户3096723
用户3096723
  • 发布:2026-06-30 11:54
  • 更新:2026-06-30 11:54
  • 阅读:13

如何解决NavDestination切换页面后浏览位置无法保存问题

分类:uni小程序sdk

问题现象
在HarmonyOS中,使用NavDestination实现页面跳转至其他页面后,下次进入如何保持跳转前页面的浏览位置?

背景知识
Navigation:Navigation组件是路由导航的根视图容器,一般作为Page页面的根容器使用,其内部默认包含了标题栏、内容区和工具栏,其中内容区默认首页显示导航内容(NavDestination的子组件)或非首页显示(NavDestination的子组件),首页和非首页通过路由进行切换。
@ohos.arkui.observer(无感监听)提供UI组件行为变化的无感监听能力。可以监听Navigation的页面切换事件进行相应操作。
Scroll是一种可滚动的容器组件,当子组件的布局尺寸超过父组件的尺寸时,内容可以滚动。
onDidScroll方法在Scroll滚动时触发,可用于在滑动过程中获取Scroll组件的偏移量yOffset。
scrollTo方法可用于让Scroll组件滑动到指定位置。
AppStorage是应用全局的UI状态存储,和应用的进程绑定,只能在UI主线程中使用,无法在子线程中使用、修改。
解决方案
方案一:使用路由模式保留页面实例。
在页面跳转时不要使用pop和clear等方法,否则NavDestination页面会被回收。可以采用单例模式MOVE_TO_TOP_SINGLETON跳转至指定的页面,该方式会使用栈内已存在的页面实例(即浏览位置不变)。

由于单例模式是从栈底到栈顶依次查找,当栈内存在多个同名页面实例时,会默认跳转最底层的同名页面实例,所以需确保栈内只存在一个同名实例,否则跳转的页面保留的滚动位置与上一次显示的页面会不一致(显示的是栈底同名实例保留的滚动位置)。

import { PageOneS1Builder } from './PageOneS1';
import { PageTwoS1Builder } from './PageTwoS1';

@Entry
@Component
struct Solution1 {
private pathStack: NavPathStack = new NavPathStack();

aboutToAppear(): void {
this.pathStack.pushPath({ name: 'PageOneS1' }); // 推送第一个子页作为首页
}

@Builder
pageMap(name: string) {
if (name === 'PageOneS1') {
PageOneS1Builder();
} else if (name === 'PageTwoS1') {
PageTwoS1Builder();
}
}

build() {
Navigation(this.pathStack) {
}
.navDestination(this.pageMap)
.width('100%')
.height('100%')
.hideNavBar(true);
}
}
@Builder
export function PageOneS1Builder() {
PageOneS1();
}

@Component
struct PageOneS1 {
private pathStack: NavPathStack = new NavPathStack();
private arr: number[] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15];

build() {
NavDestination() {
Column({ space: 20 }) {
Row() {
Button('跳转页面')
.margin({ top: 20 })
.onClick(() => {
this.pathStack.pushPathByName('PageTwoS1', null, false);
});
};

    List({ space: 16 }) {  
      ForEach(this.arr, (item: number) => {  
        ListItem() {  
          Text(item.toString())  
            .width('100%')  
            .height(100)  
            .fontSize(16)  
            .textAlign(TextAlign.Center)  
            .borderRadius(16)  
            .backgroundColor('#F1F3F5');  
        }.width('100%');  
      }, (item: string) => item);  
    }.padding({ left: 12, right: 12 })  
    .expandSafeArea([SafeAreaType.SYSTEM], [SafeAreaEdge.BOTTOM])  
    .alignListItem(ListItemAlign.Center)  
    .height('90%');  
  };  
}.width('100%').height('100%')  
.onReady((context: NavDestinationContext) => {  
  this.pathStack = context.pathStack;  
});  

}
}
@Builder
export function PageTwoS1Builder() {
PageTwoS1();
}

@Component
struct PageTwoS1 {
pathStack: NavPathStack = new NavPathStack();

build() {
NavDestination() {
Column({ space: 20 }) {
Button('单例跳转')
.width('30%')
.margin('20vp')
.onClick(() => {
// 或者使用单例模式跳转回其它页面
this.pathStack.pushPath({ name: 'PageOneS1' }, { launchMode: LaunchMode.MOVE_TO_TOP_SINGLETON });
});
}.width('100%').height('100%');
}
.onReady((context: NavDestinationContext) => {
this.pathStack = context.pathStack;
});
}
}
https://pastebin.com/B0hqJAZx
https://pastebin.com/v4JTaWDt
https://pastebin.com/d5ge4288
https://pastebin.com/3shcMPv5
https://pastebin.com/70uKbDuw
https://pastebin.com/3LhjKXni
https://pastebin.com/5gVBhCGx
https://pastebin.com/dpMxkMXN
https://pastebin.com/fv8cG0b7
https://pastebin.com/JtuLjtFR
https://pastebin.com/yCfWpnxz
https://pastebin.com/Q2rrq3WB
https://pastebin.com/3bKAPcGk
https://pastebin.com/j7MfT7Dc
https://pastebin.com/U0p3Fvrc
https://pastebin.com/6sYfe5Lr
https://pastebin.com/jLNESEFx
https://pastebin.com/2KdkqCkw
https://pastebin.com/WAgQsmsV
https://pastebin.com/xmiRqKfd
https://pastebin.com/LXPu7Btx
https://pastebin.com/Qcg8dPbD
https://pastebin.com/EhtDxXMD
https://pastebin.com/yEwh5m4v
https://pastebin.com/tU0CueV4
https://pastebin.com/8JzMaNz7
https://pastebin.com/3jg8aspW
https://pastebin.com/YyHrmrJK
https://pastebin.com/ZgagcLAc
https://pastebin.com/ABpgUr5G
https://pastebin.com/cvPYk5AN
https://pastebin.com/XGZQUT1m
https://pastebin.com/qAd27GCF
https://pastebin.com/PiFNHkLd
https://pastebin.com/aiYX5L1G
https://pastebin.com/aqpnXQvz
https://pastebin.com/kB8XJ9n2
https://pastebin.com/sLfk0TR7
https://pastebin.com/nQ9nV4g8
https://pastebin.com/NAUQ9SA0
https://pastebin.com/GxvWFg6T
https://pastebin.com/TSNy8Cys
https://pastebin.com/fpN03727
https://pastebin.com/QAFi8vrv
https://pastebin.com/fGkfHRgA
https://pastebin.com/3uF2PZgW
https://pastebin.com/yrwWGir9
https://pastebin.com/DuJE5XWa
https://pastebin.com/AVFvUePM
https://pastebin.com/M238qSej
https://pastebin.com/0B4NeJVz
https://pastebin.com/QC10Yytt
https://pastebin.com/z9uFQLbx
https://pastebin.com/jnR6Ksj2
https://pastebin.com/PJRHZYLg
https://pastebin.com/fskAwwPB
https://pastebin.com/GZ06rdYf
https://pastebin.com/spckePTy
https://pastebin.com/k5bpeqiD
https://pastebin.com/FUU83kVA
https://pastebin.com/dM5D2uq9
https://pastebin.com/HDyrbkcH
https://pastebin.com/6wLvBQBZ
https://pastebin.com/mkzrQGUE
https://pastebin.com/w40S6h1g
https://pastebin.com/EC5Qsc1J
https://pastebin.com/YyecbF2B
https://pastebin.com/rzFVRVD1
https://pastebin.com/nfukdwW3
https://pastebin.com/U0TfAD8F
https://pastebin.com/cegRdmaa
https://pastebin.com/HP55mCGz
https://pastebin.com/tDXrav7c
https://pastebin.com/VVzmD76k
https://pastebin.com/BHNwWAgu
https://pastebin.com/eR9LLY9Q
https://pastebin.com/0VYxXiXy
https://pastebin.com/XnBpR3QN
https://pastebin.com/tBtYnBVG
https://pastebin.com/8u9mvEPB
https://pastebin.com/2U7ttpz2
https://pastebin.com/Gagnm4jU
https://pastebin.com/43arJtBe
https://pastebin.com/XZ5ysaad
https://pastebin.com/JH3cJ0tN
https://pastebin.com/WfYeNrRa
https://pastebin.com/qDUK54Zy
https://pastebin.com/5LK03T2X
https://pastebin.com/d9GP4N9f
https://pastebin.com/AegGQDh5
https://pastebin.com/Msh1eGGs
https://pastebin.com/pW3QVH8H
https://pastebin.com/YQvV6CCA
https://pastebin.com/BekJgqXm
https://pastebin.com/24wFN8fj
https://pastebin.com/PcJRC4W5
https://pastebin.com/cjM2pYBz
https://pastebin.com/vEqh5L7f
https://pastebin.com/KZBzS7Re
https://pastebin.com/M7KwpPmg

0 关注 分享

要回复文章请先登录注册