
如何写精华回答,获更多曝光?
发布
import { promptAction } from "@kit.ArkUI"
const stack = new NavPathStack()
@Entry
@Component
export struct Index {
@Builder
PageMap(name: string) {
if (name === "second") {
Second()
} else if (name === "third") {
Third()
} else if (name === "forth") {
Forth()
}
}
build() {
Navigation(stack) {
Text('首页')
.margin({ top: 50 })
.onClick(() => {
stack.pushPath({
name: "second",
onPop: () => {
}
})
})
}
.mode(NavigationMode.Stack)
.navDestination(this.PageMap)
}
}
@Component
export struct Second {
build() {
NavDestination() {
Text('页面二,点击进入页面三')
.margin({ top: 50 })
.onClick(() => {
stack.pushPath({
name: "third",
onPop: (popInfo: PopInfo) => {
promptAction.showToast({
message: '返回结果为' + popInfo.result as string
})
}
})
})
}
}
}
@Component
export struct Third {
build() {
NavDestination() {
Text('页面三,点击进入页面四')
.margin({ top: 50 })
.onClick(() => {
stack.pushPath({
name: "forth",
onPop: () => {
}
})
})
}
}
}
@Component
export struct Forth {
build() {
NavDestination() {
Text('页面四,点击返回页面二')
.margin({ top: 50 })
.onClick(() => {
stack.popToName('second', 'aaaaaaaa')
})
}
}
}
如案例所示,首页->Second->Third->Forth,从Forth直接返回Second,携带的参数在onPop里接收不到