当前位置:   article > 正文

鸿蒙开发Day02_鸿蒙 pushurl clear stack

鸿蒙 pushurl clear stack
数据同步

在这里插入图片描述

页面路由

页面路由是指在应用程序中实现不同页面之间的跳转和数据传递
页面栈的最大容量上限为32个页面,使用router.clear()方法可以清空页面栈,释放内存

Router有两种页面跳转模式,分别是:

router.pushUrl(): 目标页不会替换当前页,而是压入页面栈,因此可以用router.back()返回当前页
router.replaceUrl(): 目标页替换当前页,当前页会被销毁并释放资源,无法返回当前页

Router有两种页面实例模式,分别是:

Standard: 标准实例模式,每次跳转都会新建一个目标页并压入栈顶。默认就是这种模式
Single: 单实例模式,如果目标页已经在栈中,则离栈顶最近的Url页面会被移动到栈顶并重新加载

在这里插入图片描述

router.showAlertBeforeBackPage() //返回前的警告

练习:

import router from '@ohos.router'
@Entry
@Component
struct game{
  build(){
    Column(){
      Stack(){
        Image($r("app.media.img"))
          .height('100%')
          .width('100%')
        Button(){
          Text('开始游戏')
            .fontSize(20)
            .fontColor('#ff0')
            .width(80)
            .height(40)
        }.position({x: 130,y: 550})
        .width(100)
        .onClick(()=>{
          router.replaceUrl({
            url: 'pages/game_1'
          }, router.RouterMode.Single)
        })
      }
    }
  }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
@Entry
@Component
struct game_1{
  @State imgX:number = 100
  @State imgY:number = 100
  build(){
    Column(){
      Stack(){
        Image($r("app.media.img_2"))
        Image($r("app.media.img_3"))
          .width(50)
          .position({x:this.imgX, y:this.imgY})
        Button(){
          Text("^")
            .fontSize(20)
        }.type(ButtonType.Circle)
        .width(30)
        .position({x:100, y:200})
        .onClick(()=>{
          animateTo({duration:1000},()=>{
            this.imgY -= 100
          })
        })

        Button(){
          Text("<")
            .fontSize(20)
        }
        .type(ButtonType.Circle)
        .width(30)
        .position({x:60, y:240})
        .onClick(()=>{
          animateTo({duration:1000},()=>{
            this.imgX -= 100
          })
        })

        Button(){
          Text(">")
            .fontSize(20)
        }
        .type(ButtonType.Circle)
        .width(30)
        .position({x:140, y:240})
        .onClick(()=>{
          animateTo({duration:1000},()=>{
            this.imgX += 100
          })
        })

        Button(){
          Text("-")
        }
        .width(30)
        .position({x:100, y:280})
        .onClick(()=>{
          animateTo({duration:1000},()=>{
            this.imgY += 100
          })
        })
      }
    }
  }
}
        animateTo({duration:1000},()=>{
            this.imgY += 100
          })
        })
      }
    }
  }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/煮酒与君饮/article/detail/778542
推荐阅读
相关标签
  

闽ICP备14008679号