解决措施可以在har中使用命名路由( router.pushNamedRoute )来实现示例代码MainPage@Entry({ routeName: 'MainPage' }) @Component export struct MainPage { @State message: string = 'Hello World'; build() { Row() { Column() { Text(this.message) .fontSize(30) .fontWeight(FontWeight.Bold) .backgroundColor(Color.Pink) Text(JSON.stringify(router.getParams())) .fontSize(30) .fontWeight(FontWeight.Bold) .backgroundColor(Color.Yellow) Button('跳转第二个页面') .onClick(() => { router.pushNamedRoute({ name: 'SecondPage' }) }) } .width('100%') } .height('100%') } }SecondPage@Entry({ routeName: 'SecondPage' }) @Component export struct SecondPage { @State message: string = 'SecondPage'; build() { Row() { Column() { Text(this.message) .fontSize(50) .fontWeight(FontWeight.Bold) .backgroundColor(Color.Yellow) Button('返回上一页router') .onClick(() => { console.log('返回上一页通过router') router.pushNamedRoute({ name: 'MainPage' }) }) Button('返回上一页通过back') .onClick(() => { console.log('返回上一页通过back') router.back() }) } .width('100%') } .height('100%') } } class RouterParams { private param1: string private param2: number[] constructor(param1: string, param2: number[]) { this.param1 = param1 this.param2 = param2 } } export class Test { static routerToAnother(message: string, arr: Array<number>) { router.pushNamedRoute({ name: 'MainPage', params: new RouterParams(message, arr) }) } }har中Index.etsexport { MainPage } from './src/main/ets/components/mainpage/MainPage'; export { Test } from './src/main/ets/components/mainpage/Test'; export {SecondPage}from'./src/main/ets/components/mainpage/SecondPage'外部引用@Entry @Component struct Index { @State message: string = '使用接口方法'; build() { Row() { Column() { Button(this.message) .fontSize(50) .fontWeight(FontWeight.Bold) .onClick(() => { Test.routerToAnother('message', [1, 2.3]) }) Button('使用命名路由') .fontSize(50) .fontWeight(FontWeight.Bold) .onClick(() => { router.pushNamedRoute({ name: 'MainPage' }) }) } .width('100%') } .height('100%') } }
解决措施
可以在har中使用命名路由( router.pushNamedRoute )来实现
示例代码
MainPage
SecondPage
har中Index.ets
外部引用