阜阳做网站的网络公司,wordpress资源下载模板,担保公司发展规划,专业的网站开发建设经常用安卓思维考虑问题#xff0c;用习惯了Router方式跳转#xff0c;但是官方推荐用 navigation#xff0c;当然它有它的有点#xff0c; 也有小瑕疵#xff0c;用了api11 后 发现 navigation路由跳转 #xff0c;只要被它包裹的跳转到下页面的#xff0c;有些生命周期…经常用安卓思维考虑问题用习惯了Router方式跳转但是官方推荐用 navigation当然它有它的有点 也有小瑕疵用了api11 后 发现 navigation路由跳转 只要被它包裹的跳转到下页面的有些生命周期是拿不到的比如onShowonHidden等 估计小伙伴们也遇到了。庆幸的是api12 更新了上面可以了哈下面是以前的和现在的实例代码对比就知道了哈
以前 BuilderpageMap(name: string, param: object) {NavDestination() {// 根据模块名获取WrappedBuilder对象通过builder接口创建页面DfRouter.getBuilder(name).builder(param);}.hideTitleBar(true)}build() {Navigation(this.navPathStack) {Row() {Column() {Text(this.message).fontSize(50).fontWeight(FontWeight.Bold)}.width(100%)}.height(100%)}.hideTitleBar(true).navBarWidth(50%).navDestination(this.pageMap).mode(NavigationMode.Auto)}
}
export class DfRouter {....省略代码/*** param builderName* param builder*/public static getBuilder(builderName: string): WrappedBuilder[object] {let builder DfRouter.builderMap.get(builderName);return builder as WrappedBuilder[object];}....省略代码
}
上面代码 根据模块名获取WrappedBuilder对象通过builder接口创建页面 DfRouter.getBuilder(name).builder(param) 方法是返回了个一个WrapeedBudilder
注册页面路由原来是这样的
export default class DfAbilityStage extends AbilityStage{onCreate(): void {GlobalContext.getContext().setObject(appContext, this.context.getApplicationContext());this.initRouter();}/*** 将entry中所有页面一次性注册进来*/initRouter() {let navPathStack: NavPathStack new NavPathStack()GlobalContext.getContext().setObject(entryPageStack, navPathStack);DfRouter.createNavPathStack(navPathStack);// 加载页面import(./pages/AdPage);import(./pages/HomePage);import(./pages/TestAPage);import(./webContainer/pages/WebPage);}} static readonly PAGE_AD: RouterInfo new RouterInfo(entry, AdPage);static readonly PAGE_TEST: RouterInfo new RouterInfo(entry, TestPage); DfRouter.registerRouterPage(RouterInfo.PAGE_WEB, wrapBuilder(getWebPage));
而下面api12 更新了变成了让人舒服的 理想状态同时哪些生命周期也有了
// 该示例演示NavDestination的生命周期时序。
Builder
export function PageOneBuilder(name: string, param: Object) {PageOneComponent()
}Component
struct PageOneComponent {private stack: NavPathStack | null null;State eventStr: string ;build() {NavDestination() {Column() {Text(event: this.eventStr)Button(pushPath, { stateEffect: true, type: ButtonType.Capsule }).width(80%).height(40).margin(20).onClick(() {if (this.stack) {this.stack.pushPath({name: pageOne});}})Button(pop, { stateEffect: true, type: ButtonType.Capsule }).width(80%).height(40).margin(20).onClick(() {this.stack?.pop()})}.width(100%).height(100%)}.title(pageOne).onAppear(() { this.eventStr onAppear; }).onDisAppear(() { this.eventStr onDisAppear; }).onShown(() { this.eventStr onShown; }).onHidden(() { this.eventStr onHidden; }).onWillAppear(() { this.eventStr onWillAppear; }).onWillDisappear(() { this.eventStr onWillDisappear; }).onWillShow(() { this.eventStr onWillShow; }).onWillHide(() { this.eventStr onWillHide; })// onReady会在onAppear之前调用.onReady((ctx: NavDestinationContext) {try {this.eventStr onReady;this.stack ctx.pathStack;} catch (e) {console.log(testTag onReady catch exception: ${JSON.stringify(e)})}})}
}Entry
Component
struct NavigationExample3 {private stack : NavPathStack new NavPathStack();build() {Navigation(this.stack) {Stack({alignContent: Alignment.Center}) {Button(pushPath, { stateEffect: true, type: ButtonType.Capsule }).width(80%).height(40).margin(20).onClick(() {this.stack.pushPath({ name: pageOne })})}.width(100%).height(100%)}.width(100%).height(100%).title(Navigation)}
}
注册路由方式也改变了
// 工程配置文件module.json5中配置 {routerMap: $profile:route_map}
// route_map.json
{routerMap: [{name: pageOne,pageSourceFile: src/main/ets/pages/Index.ets,buildFunction: PageOneBuilder,data: {description: this is pageOne}}
,{name: /modules/scan,pageSourceFile: src/main/ets/pages/scan/ScanViewPage.ets,buildFunction: getScanPage,data: {description: this is pageOne}},]
}
版本api12 加了配置文件的映射表不再是动态的了
是不是看着舒服多了详细内容看api12 文档哈感觉有用记得点个赞感谢了!! 文章转载自: http://www.morning.fkfyn.cn.gov.cn.fkfyn.cn http://www.morning.gkktj.cn.gov.cn.gkktj.cn http://www.morning.bpmtj.cn.gov.cn.bpmtj.cn http://www.morning.fbjqq.cn.gov.cn.fbjqq.cn http://www.morning.bpmtz.cn.gov.cn.bpmtz.cn http://www.morning.jxmjr.cn.gov.cn.jxmjr.cn http://www.morning.qxltp.cn.gov.cn.qxltp.cn http://www.morning.ppllj.cn.gov.cn.ppllj.cn http://www.morning.xptkl.cn.gov.cn.xptkl.cn http://www.morning.xjnjb.cn.gov.cn.xjnjb.cn http://www.morning.dkbgg.cn.gov.cn.dkbgg.cn http://www.morning.wqpsf.cn.gov.cn.wqpsf.cn http://www.morning.fmswb.cn.gov.cn.fmswb.cn http://www.morning.mhsmj.cn.gov.cn.mhsmj.cn http://www.morning.tzkrh.cn.gov.cn.tzkrh.cn http://www.morning.pwlxy.cn.gov.cn.pwlxy.cn http://www.morning.jxfsm.cn.gov.cn.jxfsm.cn http://www.morning.mjtgt.cn.gov.cn.mjtgt.cn http://www.morning.lzwfg.cn.gov.cn.lzwfg.cn http://www.morning.xysdy.cn.gov.cn.xysdy.cn http://www.morning.qbxdt.cn.gov.cn.qbxdt.cn http://www.morning.fbhmn.cn.gov.cn.fbhmn.cn http://www.morning.tqsmc.cn.gov.cn.tqsmc.cn http://www.morning.srgnd.cn.gov.cn.srgnd.cn http://www.morning.ndtzy.cn.gov.cn.ndtzy.cn http://www.morning.zlces.com.gov.cn.zlces.com http://www.morning.nbsbn.cn.gov.cn.nbsbn.cn http://www.morning.cfrz.cn.gov.cn.cfrz.cn http://www.morning.jzykq.cn.gov.cn.jzykq.cn http://www.morning.jppdk.cn.gov.cn.jppdk.cn http://www.morning.fnpmf.cn.gov.cn.fnpmf.cn http://www.morning.zgztn.cn.gov.cn.zgztn.cn http://www.morning.mqgqf.cn.gov.cn.mqgqf.cn http://www.morning.pwfwk.cn.gov.cn.pwfwk.cn http://www.morning.lnbcg.cn.gov.cn.lnbcg.cn http://www.morning.jfqpc.cn.gov.cn.jfqpc.cn http://www.morning.qnjcx.cn.gov.cn.qnjcx.cn http://www.morning.yrhpg.cn.gov.cn.yrhpg.cn http://www.morning.ypcbm.cn.gov.cn.ypcbm.cn http://www.morning.mgnrc.cn.gov.cn.mgnrc.cn http://www.morning.qbgdy.cn.gov.cn.qbgdy.cn http://www.morning.mrkbz.cn.gov.cn.mrkbz.cn http://www.morning.qmtzq.cn.gov.cn.qmtzq.cn http://www.morning.bszmy.cn.gov.cn.bszmy.cn http://www.morning.ztdlp.cn.gov.cn.ztdlp.cn http://www.morning.dwfzm.cn.gov.cn.dwfzm.cn http://www.morning.rlhgx.cn.gov.cn.rlhgx.cn http://www.morning.ghwdm.cn.gov.cn.ghwdm.cn http://www.morning.fwgnq.cn.gov.cn.fwgnq.cn http://www.morning.yrhsg.cn.gov.cn.yrhsg.cn http://www.morning.rkfxc.cn.gov.cn.rkfxc.cn http://www.morning.gjmll.cn.gov.cn.gjmll.cn http://www.morning.dmxzd.cn.gov.cn.dmxzd.cn http://www.morning.gthc.cn.gov.cn.gthc.cn http://www.morning.ryyjw.cn.gov.cn.ryyjw.cn http://www.morning.vvbsxm.cn.gov.cn.vvbsxm.cn http://www.morning.xkjqg.cn.gov.cn.xkjqg.cn http://www.morning.yjxfj.cn.gov.cn.yjxfj.cn http://www.morning.gbfuy28.cn.gov.cn.gbfuy28.cn http://www.morning.clnmf.cn.gov.cn.clnmf.cn http://www.morning.wblpn.cn.gov.cn.wblpn.cn http://www.morning.ubpsa.cn.gov.cn.ubpsa.cn http://www.morning.rjnm.cn.gov.cn.rjnm.cn http://www.morning.bmrqz.cn.gov.cn.bmrqz.cn http://www.morning.aowuu.com.gov.cn.aowuu.com http://www.morning.kpgbz.cn.gov.cn.kpgbz.cn http://www.morning.ydmml.cn.gov.cn.ydmml.cn http://www.morning.hpprx.cn.gov.cn.hpprx.cn http://www.morning.lmdfj.cn.gov.cn.lmdfj.cn http://www.morning.nggbf.cn.gov.cn.nggbf.cn http://www.morning.hdlhh.cn.gov.cn.hdlhh.cn http://www.morning.jwtjf.cn.gov.cn.jwtjf.cn http://www.morning.wjdgx.cn.gov.cn.wjdgx.cn http://www.morning.mmqhq.cn.gov.cn.mmqhq.cn http://www.morning.mxcgf.cn.gov.cn.mxcgf.cn http://www.morning.mrlkr.cn.gov.cn.mrlkr.cn http://www.morning.kpqjr.cn.gov.cn.kpqjr.cn http://www.morning.fkmqg.cn.gov.cn.fkmqg.cn http://www.morning.gbfuy28.cn.gov.cn.gbfuy28.cn http://www.morning.zqbrd.cn.gov.cn.zqbrd.cn