From 6c4fed1f6b3808bc9fc11ff750bb661acbba9ab1 Mon Sep 17 00:00:00 2001 From: wangyingjun01 Date: Fri, 1 Dec 2023 16:39:10 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A1=A5=E5=85=85=E8=BF=94=E5=9B=9E=E6=8C=89?= =?UTF-8?q?=E9=92=AE=E5=92=8C=E9=A1=B5=E9=9D=A2=E4=B8=8A=E4=B8=8B=E6=BB=9A?= =?UTF-8?q?=E5=8A=A8=E6=95=88=E6=9E=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: wangyingjun01 --- entry/src/main/ets/pages/Index.ets | 269 ++++++++++++++------------ entry/src/main/ets/pages/returnTo.ets | 61 +++--- entry/src/main/ets/pages/transit.ets | 150 +++++++------- 3 files changed, 249 insertions(+), 231 deletions(-) diff --git a/entry/src/main/ets/pages/Index.ets b/entry/src/main/ets/pages/Index.ets index b593700..1fcca16 100644 --- a/entry/src/main/ets/pages/Index.ets +++ b/entry/src/main/ets/pages/Index.ets @@ -19,6 +19,7 @@ import {registerInterceptor, InterceptorCallback,IInterceptor} from"@ohos/aroute import {PretreatmentService} from '@ohos/arouteronactivityresult'; import {NavigationCallback} from '@ohos/arouteronactivityresult' import { Params } from './transit'; +import router from '@ohos.router'; let iInterceptor:IInterceptor = { process(postcard:Postcard, callback:InterceptorCallback){ @@ -98,138 +99,152 @@ struct Index { @State title: string= "Route redirection" // 自定义跳转路径 @State router: string= "pages/transit" + build() { - Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center }) { - // 标题 - Text(this.title) - .fontSize(24) - .fontWeight(FontWeight.Bold) - // 正常无参跳转 - Button("正常跳转") - .width("460px") - .height("200px") - .backgroundColor("#bb5959") - .margin(10) - .onClick(() => { - Arouter.getInstance() - .build("pages/transit") - .setGreenChannel(true) - .navigation() - }) - - Row(){ - Text("参数:") - .fontSize(18) - TextInput({placeholder: "请输入需要传的参数",text: this.value}) - .width(240) - .height(40) - .margin({left: '30px'}) - .placeholderColor(Color.Blue) - .placeholderFont({size: "18fp",weight: "100"}) - .border({width: "2",color: "#c2bfbf",radius: "15"}) - .onChange((value)=>{ - this.value = value + Scroll() { + Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center }) { + Button("返回") + .width("460px") + .height("200px") + .backgroundColor("#bb5959") + .margin(10) + .onClick(() => { + router.back(); }) - } - Text("将文本框中的内容传至transit页面").fontSize(18) - // 跳转传参 - Button("跳转传参") - .width("460px") - .height("200px") - .backgroundColor("#bb5959") - .margin(10) - .onClick(() => { - let data:Params={ - index:this.value - } - Arouter.getInstance() - .build("pages/transit") - .withParams(data) - .setGreenChannel(true) - .navigation() - }) - - Text(this.index) - .fontSize(24) - // 跳转并返回数据参数 - Button("跳转回调") - .width("460px") - .height("200px") - .backgroundColor("#bb5959") - .margin(10) - .onClick(() => { - Arouter.getInstance() - .build("pages/transit") - .setGreenChannel(true) - .navigationWithCallback(callback) - }) - // 跳转拦截 - Button("跳转拦截") - .width("460px") - .height("200px") - .backgroundColor("#bb5959") - .margin(10) - .onClick(() => { - let data:Params={ - index:"Route redirection" - } - Arouter.getInstance() - .build("pages/transit") - .withParams(data) - .navigationWithCallback(callback) - }) - // 自定义跳转路径 - Row(){ - Text("自定义跳转:") - .fontSize(18) - TextInput({placeholder: "请输入需要跳转的路径",text: this.router}) - .width(240) - .height(40) - .margin({left:'30px'}) - .placeholderColor(Color.Blue) - .placeholderFont({size: "18fp",weight: "100"}) - .border({width: "2",color: "#c2bfbf",radius: "15"}) - .onChange((value) => { - this.router=value + // 标题 + Text(this.title) + .fontSize(24) + .fontWeight(FontWeight.Bold) + // 正常无参跳转 + Button("正常跳转") + .width("460px") + .height("200px") + .backgroundColor("#bb5959") + .margin(10) + .onClick(() => { + Arouter.getInstance() + .build("pages/transit") + .setGreenChannel(true) + .navigation() }) - } - Text("pages/transit或pages/returnTo") - .fontSize(20) - - Button("自定义跳转") - .width("460px") - .height("200px") - .backgroundColor("#bb5959") - .margin(10) - .onClick(()=>{ - let data:Params={ - index:"Route redirection" - } - Arouter.getInstance() - .build(this.router) - .withParams(data) - .setPretreatmentService(pretreatmentService) - .navigationWithCallback(callback) - }) - Row(){ - Text("预处理:") - .fontSize(18) - TextInput({placeholder:"",text:this.service}) - .width(240) - .height(40) - .margin({left: '30px'}) - .placeholderColor(Color.Blue) - .placeholderFont({size: "18fp",weight: "100"}) - .border({width: "2",color: "#c2bfbf",radius: "15"}) - .onChange((value)=>{ - this.service=value - service=value + + Row() { + Text("参数:") + .fontSize(18) + TextInput({ placeholder: "请输入需要传的参数", text: this.value }) + .width(240) + .height(40) + .margin({ left: '30px' }) + .placeholderColor(Color.Blue) + .placeholderFont({ size: "18fp", weight: "100" }) + .border({ width: "2", color: "#c2bfbf", radius: "15" }) + .onChange((value) => { + this.value = value + }) + } + + Text("将文本框中的内容传至transit页面").fontSize(18) + // 跳转传参 + Button("跳转传参") + .width("460px") + .height("200px") + .backgroundColor("#bb5959") + .margin(10) + .onClick(() => { + let data: Params = { + index: this.value + } + Arouter.getInstance() + .build("pages/transit") + .withParams(data) + .setGreenChannel(true) + .navigation() }) - } - Text("false不跳转,true继续跳转,其他默认为true,只对自定义跳转有效") - .fontSize(16).fontColor("red").margin({top:"10px"}) - .width("70%") + Text(this.index) + .fontSize(24) + // 跳转并返回数据参数 + Button("跳转回调") + .width("460px") + .height("200px") + .backgroundColor("#bb5959") + .margin(10) + .onClick(() => { + Arouter.getInstance() + .build("pages/transit") + .setGreenChannel(true) + .navigationWithCallback(callback) + }) + // 跳转拦截 + Button("跳转拦截") + .width("460px") + .height("200px") + .backgroundColor("#bb5959") + .margin(10) + .onClick(() => { + let data: Params = { + index: "Route redirection" + } + Arouter.getInstance() + .build("pages/transit") + .withParams(data) + .navigationWithCallback(callback) + }) + // 自定义跳转路径 + Row() { + Text("自定义跳转:") + .fontSize(18) + TextInput({ placeholder: "请输入需要跳转的路径", text: this.router }) + .width(240) + .height(40) + .margin({ left: '30px' }) + .placeholderColor(Color.Blue) + .placeholderFont({ size: "18fp", weight: "100" }) + .border({ width: "2", color: "#c2bfbf", radius: "15" }) + .onChange((value) => { + this.router = value + }) + } + + Text("pages/transit或pages/returnTo") + .fontSize(20) + + Button("自定义跳转") + .width("460px") + .height("200px") + .backgroundColor("#bb5959") + .margin(10) + .onClick(() => { + let data: Params = { + index: "Route redirection" + } + Arouter.getInstance() + .build(this.router) + .withParams(data) + .setPretreatmentService(pretreatmentService) + .navigationWithCallback(callback) + }) + Row() { + Text("预处理:") + .fontSize(18) + TextInput({ placeholder: "", text: this.service }) + .width(240) + .height(40) + .margin({ left: '30px' }) + .placeholderColor(Color.Blue) + .placeholderFont({ size: "18fp", weight: "100" }) + .border({ width: "2", color: "#c2bfbf", radius: "15" }) + .onChange((value) => { + this.service = value + service = value + }) + } + + Text("false不跳转,true继续跳转,其他默认为true,只对自定义跳转有效") + .fontSize(16).fontColor("red").margin({ top: "10px" }) + .width("70%") + + } } .width('100%') .height('100%') diff --git a/entry/src/main/ets/pages/returnTo.ets b/entry/src/main/ets/pages/returnTo.ets index 0d09294..e8911c4 100644 --- a/entry/src/main/ets/pages/returnTo.ets +++ b/entry/src/main/ets/pages/returnTo.ets @@ -52,41 +52,42 @@ let iInterceptor:IInterceptor = { @Component struct returnTo { build() { - Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { - Text('returnTo') - .fontSize(50) - .fontWeight(FontWeight.Bold) - Button("返回index") - .onClick(()=>{ - postcard.setUri("pages/Index") - let url = postcard.getUri() - router.back({ url }) - }) - Button("返回transit") - .margin({top: 10}) - .onClick(()=>{ + Scroll() { + Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { + Text('returnTo') + .fontSize(50) + .fontWeight(FontWeight.Bold) + Button("返回index") + .onClick(() => { + postcard.setUri("pages/Index") + let url = postcard.getUri() + router.back({ url }) + }) + Button("返回transit") + .margin({ top: 10 }) + .onClick(() => { - router.push({url: "pages/transit"}) - }) - Button("点击打开拦截器") - .margin({top: 10}) - .onClick(()=>{ - registerInterceptor(iInterceptor) - AlertDialog.show({ - message: '拦截器已打开', - confirm: { - value: '知道啦', - action: () => { + router.push({ url: "pages/transit" }) + }) + Button("点击打开拦截器") + .margin({ top: 10 }) + .onClick(() => { + registerInterceptor(iInterceptor) + AlertDialog.show({ + message: '拦截器已打开', + confirm: { + value: '知道啦', + action: () => { - } - }, - cancel: () => { + } + }, + cancel: () => { - } + } + }) }) - }) + } } - .width('100%') .height('100%') } diff --git a/entry/src/main/ets/pages/transit.ets b/entry/src/main/ets/pages/transit.ets index 1f52a2f..7282984 100644 --- a/entry/src/main/ets/pages/transit.ets +++ b/entry/src/main/ets/pages/transit.ets @@ -40,88 +40,90 @@ struct Transit { @State value: string = "Back to previous page" build() { - Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center }) { - Column() { - Text(`index页面传来的参数:`) - .fontSize(24) - .width("100%") - .height(34) - - Divider().color("#e1d7d7").strokeWidth(2).width(300) - // index页面传来的参数 - Text(router.getParams() ? (router.getParams() as Params).index ? (router.getParams() as Params).index.toString() : "空" : "空") - .fontColor("#7e7a7a") - .fontSize(24) - } - .alignItems(HorizontalAlign.Start) - .width(300) - .height(200) - .border({ width: "4px", color: "#c5bfbf", radius: 10 }) - .margin("20px") - - Divider().color("#888585").strokeWidth(2) - - Text("将输入框中的内容回调至index页面") - .fontSize(28) - - Row() { - Text("参数:").fontSize(18) - // 将输入框中的内容回调至index页面 - TextInput({ placeholder: "请输入需要返回的参数", text: this.value }) - .width(240) - .height(40) - .margin({ left: '30px' }) - .placeholderColor(Color.Blue) - .placeholderFont({ size: "18fp", weight: "100" }) - .border({ width: "2", color: "#c2bfbf", radius: "15" }) - .onChange((value) => { - this.value = value + Scroll() { + Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center }) { + Column() { + Text(`index页面传来的参数:`) + .fontSize(24) + .width("100%") + .height(34) + + Divider().color("#e1d7d7").strokeWidth(2).width(300) + // index页面传来的参数 + Text(router.getParams() ? (router.getParams() as Params).index ? (router.getParams() as Params).index.toString() : "空" : "空") + .fontColor("#7e7a7a") + .fontSize(24) + } + .alignItems(HorizontalAlign.Start) + .width(300) + .height(200) + .border({ width: "4px", color: "#c5bfbf", radius: 10 }) + .margin("20px") + + Divider().color("#888585").strokeWidth(2) + + Text("将输入框中的内容回调至index页面") + .fontSize(28) + + Row() { + Text("参数:").fontSize(18) + // 将输入框中的内容回调至index页面 + TextInput({ placeholder: "请输入需要返回的参数", text: this.value }) + .width(240) + .height(40) + .margin({ left: '30px' }) + .placeholderColor(Color.Blue) + .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 }) + .onClick(() => { + if (postcard != null && postcard.getNavigationCallback() != null) { + (postcard.getNavigationCallback() as NavigationCallback) + .onActivityResult(this.value) + } + router.back(); }) - } - Text("* 此按钮点击跳转回调才会有效果").fontSize(16).fontColor("red") - Button("返回上个页面并回调参数") - .width("460px") - .height("200px") - .margin({ bottom: 22 }) - .onClick(() => { - if (postcard != null && postcard.getNavigationCallback() != null) { - (postcard.getNavigationCallback() as NavigationCallback) - .onActivityResult(this.value) - } - router.back(); - }) + Button("返回上个页面") + .width("460px") + .height("200px") + .margin(22) + .onClick(() => { + router.back(); + }) - Button("返回上个页面") - .width("460px") - .height("200px") - .margin(22) - .onClick(() => { - router.back(); - }) + Button("关闭页面拦截") + .onClick(() => { + // 关闭页面拦截 + unregisterInterceptor() + AlertDialog.show({ + message: '拦截器已关闭', + confirm: { + value: '知道啦', + action: () => { - Button("关闭页面拦截") - .onClick(() => { - // 关闭页面拦截 - unregisterInterceptor() - AlertDialog.show({ - message: '拦截器已关闭', - confirm: { - value: '知道啦', - action: () => { + } + }, + cancel: () => { } - }, - cancel: () => { - - } + }) }) + Button("returnTo").onClick(() => { + Arouter.getInstance() + .build("pages/returnTo") + .navigationWithCallback(callback) }) - Button("returnTo").onClick(() => { - Arouter.getInstance() - .build("pages/returnTo") - .navigationWithCallback(callback) - }) + } } .width('100%') .height('100%') -- Gitee