HarmonyOS Navigation使用问题

项目分包,使用动态路由解耦,Navigation使用(参考的应用导航涉及:https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/bpta-application-navigation-design-V5#section0427195412313

1、在单独的har包如生活包中,在index.ets中动态引入路由页 import目前只能使用路径, 用静态常量会导致闪退,报错so。

2、在页面栈交互过程中,从A->B 在B页面通过pop携带数据回退到A页面,A页面通过Callback<PopInfo>接收到数据。如果收到数据之后需要对页面做重新渲染,有没有什么方法可以做。

3、@component里的@State这些装饰器在NavDestinationz怎么用。有没有替代的。同样涉及页面重新渲染。

HarmonyOS
2024-10-16 09:57:18
1272浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
superinsect

demo如下:

//entry 模块 Index  
// Index.ets  
@Entry  
@Component  
struct Index {  
  pageInfos: NavPathStack = new NavPathStack()  
  isUseInterception: boolean = false;  
  onBackPress(): boolean | void {  
  }  
  build() {  
    Navigation(this.pageInfos) {  
      Column() {  
        Button('pushPath PageInEntry', { stateEffect: true, type: ButtonType.Capsule })  
          .width('80%')  
          .height(40)  
          .margin(20)  
          .onClick(() => {  
            this.pageInfos.pushPath({ name: 'PageInEntry' }) //将name指定的NavDestination页面信息入栈  
          })  
        Button('pushPath PageInHsp', { stateEffect: true, type: ButtonType.Capsule })  
          .width('80%')  
          .height(40)  
          .margin(20)  
          .onClick(() => {  
            this.pageInfos.pushPath({ name: 'PageInHsp' }) //将name指定的NavDestination页面信息入栈  
          })  
        Button('pushPath PageInHar', { stateEffect: true, type: ButtonType.Capsule })  
          .width('80%')  
          .height(40)  
          .margin(20)  
          .onClick(() => {  
            this.pageInfos.pushPath({ name: 'PageInHar' }) //将name指定的NavDestination页面信息入栈  
          })  
      }  
    }.title('NavIndex')  
  }  
}
//entry模块 PageInEntry  
// PageTwo.ets  
@Builder  
export function PageInEntryBuilder(name: string, param: Object) {  
  EntryPageBuilder()  
}  
@Entry  
@Component  
export struct EntryPageBuilder {  
  pageInfos: NavPathStack = new NavPathStack();  
  @State phoneNumber: string = ''  
  
  onBackPress(): boolean | void {  
    console.log('heqian');  
  }  
  build() {  
    NavDestination() {  
      Column() {  
        Text('PageInEntry')  
        Text('phoneNumber:' + this.phoneNumber)  
      }.width('100%').height('100%')  
    }.title('PageInEntry')  
    .onReady((context: NavDestinationContext) => {  
      this.pageInfos = context.pathStack;  
      this.phoneNumber = context.pathInfo.param as string;  
      console.log("current page config info is " + JSON.stringify(context.getConfigInRouteMap()))  
    })  
  }  
}
//entry模块 router_map.json, 需要再module.json5配置"routerMap": "$profile:router_map"  
{  
"routerMap": [  
{  
"name": "PageInEntry",  
"pageSourceFile": "src/main/ets/pages/PageInEntry.ets",  
"buildFunction": "PageInEntryBuilder",  
"data": {  
"description" : "this is PageInEntry"  
}  
}  
]  
}
// HSP模块 PageInHsp.ets  
class TmpClass {  
  count: number = 10  
}  
@Builder  
export function PageInHspBuilder(name: string, param: Object) {  
  PageInHsp()  
}  
@Component  
export struct PageInHsp {  
  pageInfos: NavPathStack = new NavPathStack();  
  build() {  
    NavDestination() {  
      Column() {  
        Text('PageInHsp')  
      }  
    }.title('PageInHsp')  
    .onBackPressed(() => {  
      const popDestinationInfo = this.pageInfos.pop() // 弹出路由栈栈顶元素  
      console.log('pop' + '返回值' + JSON.stringify(popDestinationInfo))  
      return true  
    }).onReady((context: NavDestinationContext) => {  
      this.pageInfos = context.pathStack  
    })  
  }  
}
//HSP模块 router_map.json, 需要再module.json5配置"routerMap": "$profile:router_map"  
{  
"routerMap": [  
{  
"name": "PageInHsp",  
"pageSourceFile": "src/main/ets/pages/PageInHsp.ets",  
"buildFunction": "PageInHspBuilder",  
"data": {  
"description" : "this is PageInHsp"  
}  
}  
]  
}
// Har模块 PageInHar.ets  
class TmpClass {  
  count: number = 10  
}  
@Builder  
export function PageInHarBuilder(name: string, param: Object) {  
  PageInHar()  
}  
@Component  
export struct PageInHar {  
  pageInfos: NavPathStack = new NavPathStack();  
  
  build() {  
    NavDestination() {  
      Column() {  
        Text('PageInHar')  
          .onClick(() => {  
            this.pageInfos.pushPathByName('PageInEntry', '123')  
          })  
      }  
    }.title('PageInHar')  
    .onBackPressed(() => {  
      const popDestinationInfo = this.pageInfos.pop() // 弹出路由栈栈顶元素  
      console.log('pop' + '返回值' + JSON.stringify(popDestinationInfo))  
      return true  
    }).onReady((context: NavDestinationContext) => {  
      this.pageInfos = context.pathStack  
    })  
  }  
}
//Har模块 router_map.json, 需要再module.json5配置"routerMap": "$profile:router_map"  
{  
"routerMap": [  
{  
"name": "PageInHar",  
"pageSourceFile": "src/main/ets/pages/PageInHar.ets",  
"buildFunction": "PageInHarBuilder",  
"data": {  
"description" : "this is PageInHar"  
}  
}  
]  
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.
  • 109.
  • 110.
  • 111.
  • 112.
  • 113.
  • 114.
  • 115.
  • 116.
  • 117.
  • 118.
  • 119.
  • 120.
  • 121.
  • 122.
  • 123.
  • 124.
  • 125.
  • 126.
  • 127.
  • 128.
  • 129.
  • 130.
  • 131.
  • 132.
  • 133.
  • 134.
  • 135.
  • 136.
  • 137.
  • 138.
  • 139.
  • 140.
  • 141.
  • 142.
  • 143.
  • 144.
  • 145.
  • 146.
  • 147.
  • 148.
  • 149.
  • 150.
  • 151.
  • 152.
  • 153.
  • 154.
  • 155.
  • 156.
  • 157.
  • 158.
  • 159.
  • 160.
  • 161.

参考文档:https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/arkts-navigation-navigation-V5#%E7%B3%BB%E7%BB%9F%E8%B7%AF%E7%94%B1%E8%A1%A8

分享
微博
QQ
微信
回复
2024-10-16 16:51:31
相关问题
HarmonyOS Navigation 使用问题
1179浏览 • 1回复 待解决
HarmonyOS Navigation组件的使用问题
730浏览 • 1回复 待解决
HarmonyOS关于navigation问题
1512浏览 • 1回复 待解决
HarmonyOS Navigation路由问题
775浏览 • 1回复 待解决
HarmonyOS navigation使用
526浏览 • 1回复 待解决
HarmonyOS Navigation传参问题
1316浏览 • 1回复 待解决
HarmonyOS navigation的getParam问题
462浏览 • 1回复 待解决
HarmonyOS Navigation组件hideNavBar问题
660浏览 • 1回复 待解决
HarmonyOSNavigation显示dialog问题
1426浏览 • 1回复 待解决
HarmonyOS 关于Navigation组件的问题
1464浏览 • 1回复 待解决
HarmonyOS Navigation生命周期问题
726浏览 • 1回复 待解决
HarmonyOS Navigation和Tab组件问题
2083浏览 • 1回复 待解决
HarmonyOS Navigation动态跳转页面问题
831浏览 • 1回复 待解决
Navigation页面跳转的问题
1353浏览 • 1回复 待解决
HarmonyOS 关于navigation和router的问题
786浏览 • 1回复 待解决
HarmonyOS Navigation的折叠屏适配问题
877浏览 • 1回复 待解决
HarmonyOS 使用Navigation的一些疑问
1489浏览 • 1回复 待解决