中国优质的IT技术网站
专业IT技术创作平台
IT职业在线教育平台
有定义buildFunction的相关代码示例吗?
微信扫码分享
// router_map.json { "routerMap": [ { "name": "pageOne", "pageSourceFile": "src/main/ets/pages/PageOne.ets", "buildFunction": "PageOneBuilder", "data": { "description": "this is pageOne" } }, { "name": "pageTwo", "pageSourceFile": "src/main/ets/pages/PageTwo.ets", "buildFunction": "PageTwoBuilder" } ] } // PageOne.ets import { BusinessError } from '@ohos.base'; class TmpClass{ count:number = 10 } class ParamWithOp { operation: number = 1 count: number = 10 } @Builder export function PageOneBuilder(name: string, param: Object) { PageOne() } @Component export struct PageOne { pageInfo: NavPathStack = new NavPathStack(); @State message: string = 'Hello World' build() { NavDestination() { Column() { Text(this.message) .width('80%') .height(50) .margin(10) Button('pushPath', { stateEffect: true, type: ButtonType.Capsule }) .width('80%') .height(40) .margin(10) .onClick(()=>{ this.pageInfo.pushPath({name: 'pageTwo', param: new ParamWithOp(), onPop: (popInfo: PopInfo)=>{ this.message = '[pushPath]last page is: ' + popInfo.info.name + ', result: ' + JSON.stringify(popInfo.result); }}); // 将name指定的NavDestination页面信息入栈,传递的数据为param,添加接收处理结果的onPop回调。 }) Button('pushPathByName', { stateEffect: true, type: ButtonType.Capsule }) .width('80%') .height(40) .margin(10) .onClick(() => { let tmp = new TmpClass() this.pageInfo.pushPathByName('pageTwo', tmp, (popInfo: PopInfo)=>{ console.log('[pushPathByName]last page is: ' + popInfo.info.name + ', result: ' + JSON.stringify(popInfo.result)) this.message = '[pushPathByName]last page is: ' + popInfo.info.name + ', result: ' + JSON.stringify(popInfo.result); },false); // 将name指定的NavDestination页面信息入栈,传递的数据为param,添加接收处理结果的onPop回调。 }) Button('pushDestination', { stateEffect: true, type: ButtonType.Capsule }) .width('80%') .height(40) .margin(10) .onClick(()=>{ let tmp = new TmpClass() // 将name指定的NavDestination页面信息入栈,传递的数据为param,添加接收处理结果的onPop回调。 this.pageInfo.pushDestination({name: 'pageTwo', param: new ParamWithOp(), onPop: (popInfo: PopInfo)=>{ this.message = '[pushDestination]last page is: ' + popInfo.info.name + ', result: ' + JSON.stringify(popInfo.result); }}).catch((error: BusinessError)=>{ console.error(`[pushDestination]failed, error code = ${error.code}, error.message = ${error.message}.`); }).then(()=>{ console.error('[pushDestination]success.'); }); }) Button('pushDestinationByName', { stateEffect: true, type: ButtonType.Capsule }) .width('80%') .height(40) .margin(10) .onClick(()=>{ let tmp = new TmpClass() // 将name指定的NavDestination页面信息入栈,传递的数据为param,添加接收处理结果的onPop回调。 this.pageInfo.pushDestinationByName('pageTwo', tmp, (popInfo)=>{ this.message = '[pushDestinationByName]last page is: ' + popInfo.info.name + ', result: ' + JSON.stringify(popInfo.result); }).catch((error: BusinessError)=>{ console.error(`[pushDestinationByName]failed, error code = ${error.code}, error.message = ${error.message}.`); }).then(()=>{ console.error('[pushDestinationByName]success.'); }); }) Button('pushPathWithoutOnPop', { stateEffect: true, type: ButtonType.Capsule }) .width('80%') .height(40) .margin(10) .onClick(()=>{ this.pageInfo.pushPath({name: 'pageTwo', param: new ParamWithOp()}); // 将name指定的NavDestination页面信息入栈。 }) Button('pushPathByNameWithoutOnPop', { stateEffect: true, type: ButtonType.Capsule }) .width('80%') .height(40) .margin(10) .onClick(() => { let tmp = new TmpClass() this.pageInfo.pushPathByName('pageTwo', tmp); // 将name指定的NavDestination页面信息入栈,传递的数据为param。 }) Button('pushDestinationWithoutOnPop', { stateEffect: true, type: ButtonType.Capsule }) .width('80%') .height(40) .margin(10) .onClick(()=>{ let tmp = new TmpClass() // 将name指定的NavDestination页面信息入栈,传递的数据为param,添加接收处理结果的onPop回调。 this.pageInfo.pushDestination({name: 'pageTwo', param: new ParamWithOp()}) .catch((error: BusinessError)=>{ console.error(`[pushDestinationWithoutOnPop]failed, error code = ${error.code}, error.message = ${error.message}.`); }).then(()=>{ console.error('[pushDestinationWithoutOnPop]success.'); }); }) Button('pushDestinationByNameWithoutOnPop', { stateEffect: true, type: ButtonType.Capsule }) .width('80%') .height(40) .margin(10) .onClick(() => { let tmp = new TmpClass() // 将name指定的NavDestination页面信息入栈,传递的数据为param。 this.pageInfo.pushDestinationByName('pageTwo', tmp) .catch((error: BusinessError)=>{ console.error(`[pushDestinationByNameWithoutOnPop]failed, error code = ${error.code}, error.message = ${error.message}.`); }).then(()=>{ console.error('[pushDestinationByNameWithoutOnPop]success.'); }); }) Button('clear', { stateEffect: true, type: ButtonType.Capsule }) .width('80%') .height(40) .margin(10) .onClick(() => { this.pageInfo.clear(); // 清除栈中所有页面。 }) }.width('100%').height('100%') }.title('pageOne') .onBackPressed(() => { this.pageInfo.pop({number: 1}) // 弹出路由栈栈顶元素。 return true }).onReady((context: NavDestinationContext) => { this.pageInfo = context.pathStack; }) } } // PageTwo.ets class resultClass { constructor(count: number) { this.count = count; } count: number = 10 } @Builder export function PageTwoBuilder(name: string, pram: Object) { PageTwo() } @Component export struct PageTwo { pathStack: NavPathStack = new NavPathStack() build() { NavDestination() { Column() { Button('pop', { stateEffect: true, type: ButtonType.Capsule }) .width('80%') .height(40) .margin(20) .onClick(() => { this.pathStack.pop(new resultClass(1)); // 回退到上一个页面,将处理结果传入push的onPop回调中。 }) Button('popToName', { stateEffect: true, type: ButtonType.Capsule }) .width('80%') .height(40) .margin(20) .onClick(() => { this.pathStack.popToName('pageOne', new resultClass(11)); // 将第一个名为name的NavDestination页面移到栈顶,将处理结果传入push的onPop回调中。 }) Button('popToIndex', { stateEffect: true, type: ButtonType.Capsule }) .width('80%') .height(40) .margin(20) .onClick(() => { this.pathStack.popToIndex(0, new resultClass(111)); // 将index指定的NavDestination页面移到栈顶,将处理结果传入push的onPop回调中。 }) Button('popWithoutResult', { stateEffect: true, type: ButtonType.Capsule }) .width('80%') .height(40) .margin(20) .onClick(() => { this.pathStack.popToIndex(0); }) }.width('100%').height('100%') } .title('pageTwo') .onBackPressed(() => { this.pathStack.pop(new resultClass(0)); // 回退到上一个页面,将处理结果传入push的onPop回调。 return true; }) .onReady((context: NavDestinationContext) => { this.pathStack = context.pathStack }) } } // Index @Entry @Component struct NavigationExample { pageInfo: NavPathStack = new NavPathStack() aboutToAppear(): void { console.log(JSON.stringify(this.pageInfo)) } build() { Navigation(this.pageInfo) { Column() { Button('StartTest', { stateEffect: true, type: ButtonType.Capsule }) .width('80%') .height(40) .margin(20) .onClick(() => { this.pageInfo.pushPath({ name: 'pageOne' }); // 将name指定的NavDestination页面信息入栈。 }) } }.title('NavIndex') } }