HarmonyOS鸿蒙Next页面栈问题
HarmonyOS鸿蒙Next页面栈问题 页面栈最多32个,但是我有个搜索页面,不断点搜索的时候会不断的压栈,但是点后退的时候,只想保存五个的历史缓存页面,如何实现?
5 回复
import { promptAction } from '@kit.ArkUI'
@Component
struct SearchDetailPage {
@Prop searchCount: string
@Prop pageCode: number
backClick: Function = () => {
}
nextClick: Function = (info: string) => {
}
build() {
Column() {
Row() {
Button('返回上一页').onClick(() => {
this.backClick()
})
Text('当前页面编号:' + this.pageCode)
}.width('100%')
Column() {
Row() {
TextInput({
placeholder: '请输入内容',
text: this.searchCount
})
.width('524.31lpx')
.height('70lpx')
.fontSize('27lpx')
.backgroundColor("#ffffff")
.onChange((value) => {
this.searchCount = value
})
Button('搜索')
.onClick(() => {
if (!this.searchCount) {
promptAction.showToast({ message: '请输入内容', bottom: '700lpx' })
return;
}
this.nextClick(this.searchCount)
})
}
.width('100%')
.justifyContent(FlexAlign.SpaceBetween)
.padding({
left: '37lpx',
top: '11lpx',
bottom: '11lpx',
right: '15lpx'
})
}
.width('100%')
.height('100%')
.backgroundColor("#F8F8F8")
}.width('100%').height('100%').backgroundColor(Color.White)
}
}
class MyBean {
static countNum: number = 0
searchCount: string = ''
pageCode: number = ++MyBean.countNum
constructor(searchCount:string) {
this.searchCount = searchCount
}
}
@Entry
@Component
struct Page58 {
@State pageArr: Array<MyBean> = [new MyBean('xx')]
build() {
Stack() {
ForEach(this.pageArr, (item: MyBean, index: number) => {
SearchDetailPage({
searchCount: item.searchCount,
pageCode:item.pageCode,
backClick: () => {
if (this.pageArr.length == 1) {
promptAction.showToast({ message: '栈里只剩下这一个页面了', bottom: '700lpx' })
} else {
this.pageArr.pop()
}
console.info('当前栈情况:' + JSON.stringify(this.pageArr))
},
nextClick: (inputValue: string) => {
if (this.pageArr.length == 5) { //再添加就超过5个了,需要删掉第一个
this.pageArr.splice(0, 1)
}
this.pageArr.push(new MyBean(inputValue))
console.info('当前栈情况:' + JSON.stringify(this.pageArr))
}
})
})
}.width('100%').height('100%')
}
}
更多关于HarmonyOS鸿蒙Next页面栈问题的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
用Navigation吧 自己管理栈
你可以只有一个搜索页,在当前页做个栈,管理组件(把组件当前页面)
小案例:
- 名称: 示例名称
- 描述: 这是一个示例描述,用于展示如何将HTML转换为Markdown。
- 创建日期: 2023-10-01
- 状态: 活动
在HarmonyOS鸿蒙Next中,页面栈管理是应用开发的核心部分。页面栈遵循“先进后出”原则,开发者可通过router
API进行页面导航,如router.push
、router.replace
和router.back
。router.push
将新页面压入栈顶,router.replace
替换当前页面,router.back
则返回上一页面。合理管理页面栈可优化用户体验,避免内存泄漏。建议在页面生命周期方法中处理资源释放,确保应用性能。