diff --git a/AppScope/app.json5 b/AppScope/app.json5 index 028cc377bc9562a0e9897654858e2930a62102ca..3d75b9c06be43c4d85ec91d52be327c45e78a2ac 100644 --- a/AppScope/app.json5 +++ b/AppScope/app.json5 @@ -3,7 +3,7 @@ "bundleName": "cn.openharmony.arouteronactivityresult", "vendor": "example", "versionCode": 1000000, - "versionName": "1.0.0", + "versionName": "2.1.0", "icon": "$media:app_icon", "label": "$string:app_name", "distributedNotificationEnabled": true diff --git a/CHANGELOG.md b/CHANGELOG.md index 02d912710c97ab66ce0bd2dd223c0b372c03d1ac..243bbfd61c4e602b669c538680aadc8578b3dae6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +### 2.1.0 + +1.update: 适配ArkTS语法规则 +2.接口使用方式变更:GlobalContext替代globalThis + ### 2.0.0 1.feature: 更新页面跳转api(router) diff --git a/arouteronactivityresult/index.ets b/arouteronactivityresult/index.ets index c8895b1606c5e806766fdbf4331c6b1a81fe2e82..552b58794bde751861de94210b45f8c540d5d402 100644 --- a/arouteronactivityresult/index.ets +++ b/arouteronactivityresult/index.ets @@ -17,8 +17,9 @@ export {IInterceptor,InterceptorCallback} from "./src/main/ets/components/arout export {navigationWithInterceptor,registerInterceptor,unregisterInterceptor} from "./src/main/ets/components/arouter/interceptorService" export {clear} from "./src/main/ets/components/arouter/logisticsCeter" export {NavigationCallback} from "./src/main/ets/components/arouter/navigationCallback" -export {Postcard} from "./src/main/ets/components/arouter/Postcard" +export {Postcard} from "./src/main/ets/components/arouter/postcard" export {PretreatmentService} from "./src/main/ets/components/arouter/pretreatmenService" +export {GlobalContext} from "./src/main/ets/components/arouter/GlobalContext" diff --git a/arouteronactivityresult/oh-package.json5 b/arouteronactivityresult/oh-package.json5 index 5924b2b56f8a084c05f421cef07e7f20e1a9a250..52aef64bca0cecc645dbb9bcd1d0d08f1a7cda25 100644 --- a/arouteronactivityresult/oh-package.json5 +++ b/arouteronactivityresult/oh-package.json5 @@ -12,7 +12,7 @@ "main": "index.ets", "repository": "https://gitee.com/openharmony-tpc/arouter-api-onActivityResult", "type": "module", - "version": "2.0.0", + "version": "2.1.0", "dependencies": {}, "tags": [ "Tool" diff --git a/arouteronactivityresult/src/main/ets/components/arouter/arouter.ets b/arouteronactivityresult/src/main/ets/components/arouter/arouter.ets index 13434d02d0649554a1a1c2c5ff4d56aff4cf8dbe..98cd15a154a97b0073e3ed7d0b8cdb68fe2af5c1 100644 --- a/arouteronactivityresult/src/main/ets/components/arouter/arouter.ets +++ b/arouteronactivityresult/src/main/ets/components/arouter/arouter.ets @@ -13,16 +13,17 @@ * limitations under the License. */ -import {Postcard} from './Postcard'; +import {Postcard} from './postcard'; import {navigationWithInterceptor} from "./interceptorService" import {PretreatmentService} from './pretreatmenService' import {NavigationCallback} from './navigationCallback' import router from '@ohos.router'; -import {GlobalContext} from './GlobalContext' +import { GlobalContext } from './GlobalContext'; + export class Arouter{ build(uri:string):Postcard{ - return new Postcard(uri, undefined); + return new Postcard(uri, new Object()); } @@ -34,7 +35,7 @@ export class Arouter{ return Arouter.sInstance } - getPostcard(path:string):Postcard{ + getPostcard(path:string):Postcard { let routes:Map = GlobalContext.getContext().getObject("routes") as Map if(!!routes ){ let routesd:Postcard |undefined = routes.get(path); @@ -55,28 +56,27 @@ export class Arouter{ GlobalContext.getContext().setObject("routes", new Map()); routes = GlobalContext.getContext().getObject("routes") as Map; } - routes.set(postcard.getUri(), postcard); } navigation(postcard:Postcard,callback:NavigationCallback) { postcard.setNavigationCallback(callback); - let pretreatmentService:PretreatmentService = postcard.getPretreatmentService(); - if(pretreatmentService != null && !pretreatmentService.onPretreatment(postcard)){ - //return null; - } - navigationWithInterceptor(postcard) + let pretreatmentService:PretreatmentService = postcard.getPretreatmentService() as PretreatmentService; + if(pretreatmentService != null && !pretreatmentService.onPretreatment(postcard)){ + }else { + navigationWithInterceptor(postcard) + } } _navigation(postcard:Postcard){ this.inject(postcard); if(postcard.getFlags()){ - router.replaceUrl({ + router.replace({ url:postcard.getUri(), params:postcard.getParams() }) } else { - router.pushUrl({ + router.push({ url:postcard.getUri(), params:postcard.getParams() }) diff --git a/arouteronactivityresult/src/main/ets/components/arouter/interceptorService.ets b/arouteronactivityresult/src/main/ets/components/arouter/interceptorService.ets index 5e9dd318385c9682bee8027d8e2e45c73e6ea683..35eb49777225144ffebb89267a0193c884486a23 100644 --- a/arouteronactivityresult/src/main/ets/components/arouter/interceptorService.ets +++ b/arouteronactivityresult/src/main/ets/components/arouter/interceptorService.ets @@ -12,17 +12,17 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -import {Postcard} from './Postcard'; +import {Postcard} from './postcard'; import {Arouter} from "./arouter"; -import {GlobalContext} from './GlobalContext' +import { GlobalContext } from './GlobalContext'; -export class IInterceptor{ - public process(postcard:Postcard,callback:InterceptorCallback){}; +export interface IInterceptor{ + process:(postcard:Postcard,callback:InterceptorCallback)=>void; } -export class InterceptorCallback{ - public onContinue(postcard:Postcard){}; - public onInterrupt(postcard:Postcard){}; +export interface InterceptorCallback{ + onContinue:(postcard:Postcard)=>void; + onInterrupt:(postcard:Postcard)=>void; } @@ -56,18 +56,18 @@ class Interceptor{ private callback:InterceptorCallback private iInterceptor:IInterceptor - constructor(iInterceptor:IInterceptor){ this.iInterceptor = iInterceptor; - this.callback = new InterceptorCallback() - this.callback.onInterrupt = (postcard:Postcard)=>{ - if(postcard != null && postcard.getNavigationCallback() != null){ - postcard.getNavigationCallback()?.onInterrupt(postcard) + this.callback = { + onInterrupt(postcard:Postcard){ + if(postcard != null && postcard.getNavigationCallbcak() != null){ + postcard.getNavigationCallbcak()!.onInterrupt(postcard) + } + }, + onContinue(postcard:Postcard){ + Arouter.getInstance()._navigation(postcard) } - } - this.callback.onContinue = (postcard:Postcard)=>{ - Arouter.getInstance()._navigation(postcard) - } + }; GlobalContext.getContext().setObject("interceptor", this); }; diff --git a/arouteronactivityresult/src/main/ets/components/arouter/logisticsCeter.ets b/arouteronactivityresult/src/main/ets/components/arouter/logisticsCeter.ets index 242af437c49e4f65fd4315dfbdea2d1f3f8121cf..d55b76a61d92f9bcfeb8817cb90f49fb5f295f8c 100644 --- a/arouteronactivityresult/src/main/ets/components/arouter/logisticsCeter.ets +++ b/arouteronactivityresult/src/main/ets/components/arouter/logisticsCeter.ets @@ -12,7 +12,8 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -import {GlobalContext} from './GlobalContext' +import { GlobalContext } from './GlobalContext'; + export function clear(){ GlobalContext.getContext().setObject("routes", null); GlobalContext.getContext().setObject("interceptor", null); diff --git a/arouteronactivityresult/src/main/ets/components/arouter/navigationCallback.ets b/arouteronactivityresult/src/main/ets/components/arouter/navigationCallback.ets index ea2ae9c24c8c1d66a16e641a418c792e92bac29d..3a2cf7f7ad0e959c9ff94cc09dda3ad275a751f3 100644 --- a/arouteronactivityresult/src/main/ets/components/arouter/navigationCallback.ets +++ b/arouteronactivityresult/src/main/ets/components/arouter/navigationCallback.ets @@ -13,16 +13,10 @@ * limitations under the License. */ -import { Postcard } from './Postcard' +import { Postcard } from './postcard' -export class NavigationCallback { - - public onArrival(postcard: Postcard): void { - } - - public onInterrupt(postcard: Postcard): void { - } - - public onActivityResult(data:string): void { - } +export interface NavigationCallback { + onArrival: (postcard: Postcard) => void; + onInterrupt: (postcard: Postcard) => void; + onActivityResult: (data: string) => void; } \ No newline at end of file diff --git a/arouteronactivityresult/src/main/ets/components/arouter/Postcard.ets b/arouteronactivityresult/src/main/ets/components/arouter/postcard.ets similarity index 49% rename from arouteronactivityresult/src/main/ets/components/arouter/Postcard.ets rename to arouteronactivityresult/src/main/ets/components/arouter/postcard.ets index 5c037bf0ecf29acf023cdc93cd3a38c749611788..1c6ecf7b9dd58f4068731995e6f5d909ac3b2d30 100644 --- a/arouteronactivityresult/src/main/ets/components/arouter/Postcard.ets +++ b/arouteronactivityresult/src/main/ets/components/arouter/postcard.ets @@ -13,128 +13,130 @@ * limitations under the License. */ -import {NavigationCallback} from './navigationCallback' -import {PretreatmentService} from './pretreatmenService'; -import {Arouter} from "./arouter"; +import { NavigationCallback } from './navigationCallback' +import { PretreatmentService } from './pretreatmenService'; +import { Arouter } from "./arouter"; export class Postcard { - private uri:string - private params?:Object - private navigationCallback?:NavigationCallback ; - private pretreatmentService:PretreatmentService = new PretreatmentService(); - private greenChannel:boolean = false; - private tag?:Object; - private flags:boolean = false; - - constructor(uri:string, params:Object | undefined){ + private uri: string + private params?: Object + private navigationCallback: NavigationCallback | null = null + private pretreatmentService: PretreatmentService | null = null + private greenChannel: boolean = false; + private tag?: Object; + private flags: boolean = false; + + constructor(uri: string, params: Object) { this.uri = uri; this.params = params; } - setPretreatmentService(pretreatmentService:PretreatmentService):Postcard{ + setPretreatmentService(pretreatmentService: PretreatmentService): Postcard { this.pretreatmentService = pretreatmentService; return this; } - getPretreatmentService(){ - return this.pretreatmentService + getPretreatmentService(): PretreatmentService | null { + if (!!this.pretreatmentService) + return this.pretreatmentService + else + return null } - setNavigationCallback(callback:NavigationCallback) { + setNavigationCallback(callback: NavigationCallback) { this.navigationCallback = callback; } - getNavigationCallback():NavigationCallback{ - if(!!this.navigationCallback){ + getNavigationCallbcak(): NavigationCallback | null { + if (!!this.navigationCallback) return this.navigationCallback - }else { - return new NavigationCallback(); - } + else + return null } - setUri(uri:string):Postcard{ + setUri(uri: string): Postcard { this.uri = uri; return this; } - withParams(params:Object):Postcard{ + withParams(params: Object): Postcard { this.params = params; return this; } - setGreenChannel(greenChannel:boolean):Postcard{ + setGreenChannel(greenChannel: boolean): Postcard { this.greenChannel = greenChannel; return this; } - getGreenChannel():boolean{ + getGreenChannel(): boolean { return this.greenChannel } getParams(): Object { - if(!!this.params){ + if (!!this.params) { return this.params; - }else { - return Object; + } else { + return new Object(); } - } - getUri():string{ + getUri(): string { return this.uri; } + // 正常跳转 - navigation(){ + navigation() { this.routers() } + // 跳转回调 - navigationWithCallback(callback:NavigationCallback) { + navigationWithCallback(callback: NavigationCallback) { this.setNavigationCallback(callback); this.routers() } - getTag():Object | void{ - if(!!this.tag){ + getTag(): Object | void { + if (!!this.tag) { return this.tag; } - } - setTag(tag:Object):Postcard{ + setTag(tag: Object): Postcard { this.tag = tag; return this; } - withFlags(flags:boolean):Postcard { + withFlags(flags: boolean): Postcard { this.flags = flags; return this; } - getFlags():boolean { + getFlags(): boolean { return this.flags; } - toString():string { + toString(): string { return "Postcard{" + "uri=" + this.uri + ", tag=" + this.tag + - ", params=" + this.params?.toString() + + ", params=" + this.params!.toString() + ", flags=" + this.flags + ", greenChannel=" + this.greenChannel + "}\n"; } - private routers(){ - Arouter.getInstance().navigation(this, this.getNavigationCallback()) + private routers() { + Arouter.getInstance().navigation(this, this.getNavigationCallbcak() as NavigationCallback) } private isIntNum(val: string) { - let regPos:RegExp = new RegExp("/ ^\d+$/"); // 非负整数 - let regNeg:RegExp = new RegExp("/^\-[1-9][0-9]*$/"); // 负整数 - if(regPos.test(val) && regNeg.test(val)){ - return true; - }else{ - return false; + let regPos: RegExp = new RegExp("/ ^\d+$/"); // 非负整数 + let regNeg: RegExp = new RegExp("/^\-[1-9][0-9]*$/"); // 负整数 + if (regPos.test(val) && regNeg.test(val)) { + return true; + } else { + return false; } } } \ No newline at end of file diff --git a/arouteronactivityresult/src/main/ets/components/arouter/pretreatmenService.ets b/arouteronactivityresult/src/main/ets/components/arouter/pretreatmenService.ets index ca3f0eeda1b0a886d14341c6a202ca5461d818e0..aa3563dc0319e8a8509f52a7449e7455eccd94d4 100644 --- a/arouteronactivityresult/src/main/ets/components/arouter/pretreatmenService.ets +++ b/arouteronactivityresult/src/main/ets/components/arouter/pretreatmenService.ets @@ -13,10 +13,7 @@ * limitations under the License. */ -import { Postcard } from './Postcard' - -export class PretreatmentService { - public onPretreatment(postcard:Postcard):boolean{ - return false; - }; +import { Postcard } from './postcard' +export interface PretreatmentService { + onPretreatment:(postcard:Postcard)=>boolean; } \ No newline at end of file diff --git a/entry/oh-package.json5 b/entry/oh-package.json5 index 6bf9ea5203cd308bb17edc44d280b2f91e8d869d..91769bf72b67547dc46fb701c5214fd7df1a03e8 100644 --- a/entry/oh-package.json5 +++ b/entry/oh-package.json5 @@ -4,7 +4,7 @@ "name": "entry", "description": "example description", "repository": {}, - "version": "1.0.0", + "version": "2.1.0", "dependencies": { "@ohos/arouteronactivityresult": "file:../arouteronactivityresult" } diff --git a/entry/src/main/ets/entryability/EntryAbility.ts b/entry/src/main/ets/entryability/EntryAbility.ts index 4aa18130e124218d77d314f335b0f39aff54237b..be8283b67cade448a872833f28491b0fe59c680d 100644 --- a/entry/src/main/ets/entryability/EntryAbility.ts +++ b/entry/src/main/ets/entryability/EntryAbility.ts @@ -13,44 +13,54 @@ * limitations under the License. */ -import UIAbility from '@ohos.app.ability.UIAbility'; import hilog from '@ohos.hilog'; -import window from '@ohos.window'; +import UIAbility from '@ohos.app.ability.UIAbility'; +import Window from '@ohos.window' export default class EntryAbility extends UIAbility { onCreate(want, launchParam) { + hilog.isLoggable(0x0000, 'testTag', hilog.LogLevel.INFO); hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); + hilog.info(0x0000, 'testTag', '%{public}s', 'want param:' + JSON.stringify(want) ?? ''); + hilog.info(0x0000, 'testTag', '%{public}s', 'launchParam:' + JSON.stringify(launchParam) ?? ''); } onDestroy() { + hilog.isLoggable(0x0000, 'testTag', hilog.LogLevel.INFO); hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy'); } - onWindowStageCreate(windowStage: window.WindowStage) { + onWindowStageCreate(windowStage: Window.WindowStage) { // Main window is created, set main page for this ability + hilog.isLoggable(0x0000, 'testTag', hilog.LogLevel.INFO); hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); windowStage.loadContent('pages/Index', (err, data) => { if (err.code) { + hilog.isLoggable(0x0000, 'testTag', hilog.LogLevel.ERROR); hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); return; } + hilog.isLoggable(0x0000, 'testTag', hilog.LogLevel.INFO); hilog.info(0x0000, 'testTag', 'Succeeded in loading the content. Data: %{public}s', JSON.stringify(data) ?? ''); }); } onWindowStageDestroy() { // Main window is destroyed, release UI related resources + hilog.isLoggable(0x0000, 'testTag', hilog.LogLevel.INFO); hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); } onForeground() { // Ability has brought to foreground + hilog.isLoggable(0x0000, 'testTag', hilog.LogLevel.INFO); hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground'); } onBackground() { // Ability has back to background + hilog.isLoggable(0x0000, 'testTag', hilog.LogLevel.INFO); hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground'); } -} \ No newline at end of file +} diff --git a/entry/src/main/ets/pages/Index.ets b/entry/src/main/ets/pages/Index.ets index 5c08a5ef39921879cbce377e5ace39de139ce9eb..b593700d3a171b2d13e9a2b3bcb671fbbdd68b65 100644 --- a/entry/src/main/ets/pages/Index.ets +++ b/entry/src/main/ets/pages/Index.ets @@ -18,65 +18,74 @@ import {Postcard} from "@ohos/arouteronactivityresult" import {registerInterceptor, InterceptorCallback,IInterceptor} from"@ohos/arouteronactivityresult" import {PretreatmentService} from '@ohos/arouteronactivityresult'; import {NavigationCallback} from '@ohos/arouteronactivityresult' +import { Params } from './transit'; -let iInterceptor = new IInterceptor(); -iInterceptor.process = (postcard:Postcard, callback:InterceptorCallback)=>{ - if(postcard.getUri() === 'pages/transit' || postcard.getUri() === 'pages/returnTo'){ - AlertDialog.show( - { - title: '', - message: '被拦截了,点击继续跳转', - primaryButton: { - value: '取消', - action: () => { - callback.onInterrupt(postcard) - } - }, - secondaryButton: { - value: '继续', - action: () => { - callback.onContinue(postcard); - } - }, - cancel: () => { +let iInterceptor:IInterceptor = { + process(postcard:Postcard, callback:InterceptorCallback){ + if(postcard.getUri() === 'pages/transit' || postcard.getUri() === 'pages/returnTo'){ + AlertDialog.show( + { + title: '', + message: '被拦截了,点击继续跳转', + primaryButton: { + value: '取消', + action: () => { + callback.onInterrupt(postcard) + } + }, + secondaryButton: { + value: '继续', + action: () => { + callback.onContinue(postcard); + } + }, + cancel: () => { + } } - } - ) - } else { - callback.onContinue(postcard); + ) + } else { + callback.onContinue(postcard); + } } } - -class C1 { - data: string = "" +interface ReturnData{ + data:string } - -let returnData:C1 = { +let returnData:ReturnData = { data:"" } -let callback = new NavigationCallback(); -callback.onActivityResult = (data)=>{ - returnData.data = data -} +let callback:NavigationCallback = { + onInterrupt(postcard:Postcard){ + + }, + onArrival(postcard:Postcard){ + }, + onActivityResult(data){ + returnData.data = data + } +}; let service="true" -let pretreatmentService = new PretreatmentService(); -pretreatmentService.onPretreatment = (postcard:Postcard)=>{ - switch(service){ - case "true": - return true - break - case "false": - return false - break - default: - return true - break +let pretreatmentService:PretreatmentService = { + onPretreatment(postcard:Postcard):boolean{ + switch(service){ + case "true": + return true + break + case "false": + return false + break + default: + return true + break + } } } + + @Entry @Component struct Index { @@ -130,9 +139,12 @@ struct Index { .backgroundColor("#bb5959") .margin(10) .onClick(() => { + let data:Params={ + index:this.value + } Arouter.getInstance() .build("pages/transit") - .withParams(this.value) + .withParams(data) .setGreenChannel(true) .navigation() }) @@ -158,9 +170,12 @@ struct Index { .backgroundColor("#bb5959") .margin(10) .onClick(() => { + let data:Params={ + index:"Route redirection" + } Arouter.getInstance() .build("pages/transit") - .withParams( "Route redirection") + .withParams(data) .navigationWithCallback(callback) }) // 自定义跳转路径 @@ -187,9 +202,12 @@ struct Index { .backgroundColor("#bb5959") .margin(10) .onClick(()=>{ + let data:Params={ + index:"Route redirection" + } Arouter.getInstance() .build(this.router) - .withParams("Route redirection") + .withParams(data) .setPretreatmentService(pretreatmentService) .navigationWithCallback(callback) }) diff --git a/entry/src/main/ets/pages/returnTo.ets b/entry/src/main/ets/pages/returnTo.ets index 54f04431fe1a26d0858e0343f52acd32def1ba69..bed92dfbaaf47e711912b61cddb56908f48e39a8 100644 --- a/entry/src/main/ets/pages/returnTo.ets +++ b/entry/src/main/ets/pages/returnTo.ets @@ -13,11 +13,13 @@ * limitations under the License. */ import router from '@ohos.router'; -import {registerInterceptor, IInterceptor, InterceptorCallback} from "@ohos/arouteronactivityresult"; +import {registerInterceptor, IInterceptor, InterceptorCallback, + NavigationCallback} from "@ohos/arouteronactivityresult"; import {Postcard} from '@ohos/arouteronactivityresult'; import {Arouter} from "@ohos/arouteronactivityresult"; -let iInterceptor = new IInterceptor() -iInterceptor.process = (postcard:Postcard, callback:InterceptorCallback)=>{ + +let iInterceptor:IInterceptor = { + process(postcard:Postcard, callback:InterceptorCallback){ if(postcard.getUri() === 'pages/transit' || postcard.getUri() === 'pages/returnTo'){ AlertDialog.show( { @@ -43,6 +45,7 @@ iInterceptor.process = (postcard:Postcard, callback:InterceptorCallback)=>{ } else { callback.onContinue(postcard); } + } } @Entry @@ -60,7 +63,8 @@ struct returnTo { Button("返回transit") .margin({top: 10}) .onClick(()=>{ - router.pushUrl({url: "pages/transit"}) + + router.push({url: "pages/transit"}) }) Button("点击打开拦截器") .margin({top: 10}) @@ -85,12 +89,13 @@ struct returnTo { .height('100%') } - onPageShow(){ + onPageShow():Postcard|void{ if (postcard == null) { postcard = Arouter.getInstance().getPostcard(router.getState().path + router.getState().name); - if(postcard != null && postcard.getNavigationCallback() != null){ - postcard.getNavigationCallback().onArrival(postcard); + if(postcard != null && postcard.getNavigationCallbcak() != null){ + (postcard.getNavigationCallbcak() as NavigationCallback).onArrival(postcard); } + return postcard; } } diff --git a/entry/src/main/ets/pages/transit.ets b/entry/src/main/ets/pages/transit.ets index e6462aaad874c67f5d0648b3ceeec8ca07be6df9..1ba49c472305870d21bdb1eaf65345835aa37385 100644 --- a/entry/src/main/ets/pages/transit.ets +++ b/entry/src/main/ets/pages/transit.ets @@ -13,22 +13,35 @@ * limitations under the License. */ import router from '@ohos.router'; -import {Arouter} from "@ohos/arouteronactivityresult"; -import {unregisterInterceptor} from "@ohos/arouteronactivityresult" -import {Postcard} from "@ohos/arouteronactivityresult" -import {NavigationCallback} from '@ohos/arouteronactivityresult' +import { Arouter, NavigationCallback } from "@ohos/arouteronactivityresult"; +import { unregisterInterceptor } from "@ohos/arouteronactivityresult" +import { Postcard } from "@ohos/arouteronactivityresult" -let callback = new NavigationCallback(); +let callback:NavigationCallback = { + onInterrupt(postcard) { + + }, + onArrival(postcard) { + + }, + onActivityResult(data) { + + } +}; + +export interface Params{ + index:string +} @Entry @Component struct Transit { // index页面传来的参数 - @State value: string= "Back to previous page" + @State value: string = "Back to previous page" build() { - Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center}) { - Column(){ + Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center }) { + Column() { Text(`index页面传来的参数:`) .fontSize(24) .width("100%") @@ -36,13 +49,14 @@ struct Transit { Divider().color("#e1d7d7").strokeWidth(2).width(300) // index页面传来的参数 - Text(router.getParams()? router.getParams()? router.getParams().toString(): "空": "空") + Text(router.getParams() ? (router.getParams() as Params).index ? (router.getParams() as Params).index.toString() : "空" : "空") .fontColor("#7e7a7a") .fontSize(24) - }.alignItems(HorizontalAlign.Start) + } + .alignItems(HorizontalAlign.Start) .width(300) .height(200) - .border({width: "4px",color: "#c5bfbf",radius: 10}) + .border({ width: "4px", color: "#c5bfbf", radius: 10 }) .margin("20px") Divider().color("#888585").strokeWidth(2) @@ -50,28 +64,29 @@ struct Transit { Text("将输入框中的内容回调至index页面") .fontSize(28) - Row(){ + Row() { Text("参数:").fontSize(18) // 将输入框中的内容回调至index页面 - TextInput({placeholder:"请输入需要返回的参数",text: this.value}) + TextInput({ placeholder: "请输入需要返回的参数", text: this.value }) .width(240) .height(40) - .margin({left:'30px'}) + .margin({ left: '30px' }) .placeholderColor(Color.Blue) - .placeholderFont({size: "18fp",weight: "100"}) - .border({width: "2",color: "#c2bfbf",radius: "15"}) + .placeholderFont({ size: "18fp", weight: "100" }) + .border({ width: "2", color: "#c2bfbf", radius: "15" }) .onChange((value) => { this.value = value }) } + Text("* 此按钮点击跳转回调才会有效果").fontSize(16).fontColor("red") Button("返回上个页面并回调参数") .width("460px") .height("200px") - .margin({bottom:22}) + .margin({ bottom: 22 }) .onClick(() => { - if(postcard != null && postcard.getNavigationCallback() != null){ - postcard.getNavigationCallback() + if (postcard != null && postcard.getNavigationCallbcak() != null) { + (postcard.getNavigationCallbcak() as NavigationCallback) .onActivityResult(this.value) } router.back(); @@ -86,7 +101,7 @@ struct Transit { }) Button("关闭页面拦截") - .onClick(()=>{ + .onClick(() => { // 关闭页面拦截 unregisterInterceptor() AlertDialog.show({ @@ -111,11 +126,12 @@ struct Transit { .width('100%') .height('100%') } - onPageShow(){ + + onPageShow() { if (postcard == null) { postcard = Arouter.getInstance().getPostcard(router.getState().path + router.getState().name); - if(postcard != null && postcard.getNavigationCallback() != null){ - postcard.getNavigationCallback().onArrival(postcard); + if (postcard != null && postcard.getNavigationCallbcak() != null) { + (postcard.getNavigationCallbcak() as NavigationCallback).onArrival(postcard); } } } @@ -127,4 +143,5 @@ struct Transit { .slide(SlideEffect.Left) } } + let postcard:Postcard; diff --git a/oh-package.json5 b/oh-package.json5 index 2532bfbcada6de46bb318117772a1aeb9e36f196..d5effcc215cde4df567f054e2f01574874324015 100644 --- a/oh-package.json5 +++ b/oh-package.json5 @@ -6,6 +6,6 @@ "name": "arouter-api-onactivityresult", "description": "example description", "repository": {}, - "version": "1.0.0", + "version": "2.1.0", "dependencies": {} } \ No newline at end of file