Ability内页面的跳转和数据传递(router和want显/隐跳转)

目录

案例:使用router完成页面跳转

1.创建一个Arkts项目

2.创建第二个页面

3.手动创建第三个页面

4.编写跳转路由

5.编写接受路由

6.编写返回上一个页面的代码

7.第三个界面代码完善

8.效果

案例:使用want启动Ability

1.创建一个新的项目

2.创建第二个界面

3.创建一个Ability

4.修改新生成的Ability中的信息

5.编写Index.ets中的代码

6.效果图

7.编写隐式启动代码

1)编写module.json5文件

2)编写index.ets中的内容

8.隐式启动效果图


案例:使用router完成页面跳转

1.创建一个Arkts项目

打开DevEco Studio开发工具 ====》 点击Create Project ===》创建项目

选中Empty Ability ===> 点击next ===>

在新弹出的界面填写相关信息===》点击finsh ===>等待界面打开

2.创建第二个页面

找到page ==>鼠标右键 ==》new ==> page ====>Empty View==>在新弹出的界面====》点击finsh ===>等待创建就可以了

3.手动创建第三个页面

在新弹出的界面编写代码

TypeScript 复制代码
@Entry   //页面入口标记
@Component
struct MyDivPage {
  build() {
    Column(){
      Text("这是我手动创建的页面")
      .id('text')
      .fontSize(50)
      .fontWeight(FontWeight.Bold)
      .alignRules({
        center: { anchor: '__container__', align: VerticalAlign.Center },
        middle: { anchor: '__container__', align: HorizontalAlign.Center }
      })
    }
      .width('100%')
      .height('100%')
    
  }
}

预览效果

在resources ===》base ==> profile ===> 双击 main_pages.json ==>配置手动创建的页面

4.编写跳转路由

1--导入路由

复制代码
import router from '@ohos.router';

2--编写代码

TypeScript 复制代码
import router from '@ohos.router';

@Entry
@Component
struct Index {
  @State message: string = '这个是首页';

  build() {
    Column() {
      Text(this.message)
        .id('HelloWorld')
        .fontSize(50)
        .fontWeight(FontWeight.Bold)
        .alignRules({
          center: { anchor: '__container__', align: VerticalAlign.Center },
          middle: { anchor: '__container__', align: HorizontalAlign.Center }
        })


      Text()
        .backgroundImage($r('app.media.girl_two'))
        .width(300)
        .height(300)


      //按钮跳转携带参数跳转页面
      Button("跳转携带参数")
        .onClick(() => {

          router.push({
            url: 'pages/Page',
            params: {
              name: '惜.己',
              age: 18
            }
          })


        })
    }
    .height('100%')
    .width('100%')
  }
}

5.编写接受路由

结构对象中的参数 param['属性名']

@State params: object = router.getParams();

name: string = params[my_name]

TypeScript 复制代码
import router from '@ohos.router';

@Entry
@Component
struct Page {
  @State message: string = '这是这是第二个页面';
  //手动接受跳转过来的数据
  @State params: object =  router.getParams();


  build() {
    Column() {
      Text(this.message)
        .id('PageHelloWorld')
        .fontSize(50)
        .fontWeight(FontWeight.Bold)
        .alignRules({
          center: { anchor: '__container__', align: VerticalAlign.Center },
          middle: { anchor: '__container__', align: HorizontalAlign.Center }
        })

      Text("名字是:"+this.params['my_name']+"\n年龄:"+this.params['my_age'])
        .id('name')


      Button("不带参数跳转")
        .onClick(()=>{
          router.pushUrl({
            url: 'pages/MyDivPage'
          })
        })

      Button("返回上一个界面")
        .onClick(()=>{
          router.back()
        })

    }
    .height('100%')
    .width('100%')
  }
}

6.编写返回上一个页面的代码

复制代码
Button("返回上一个界面")
  .onClick(()=>{
   router.back()
  })

第二个界面完善的代码

TypeScript 复制代码
import router from '@ohos.router';

@Entry
@Component
struct Page {
  @State message: string = '这是这是第二个页面';
  //手动接受跳转过来的数据
  @State params: object =  router.getParams();


  build() {
    Column() {
      Text(this.message)
        .id('PageHelloWorld')
        .fontSize(50)
        .fontWeight(FontWeight.Bold)
        .alignRules({
          center: { anchor: '__container__', align: VerticalAlign.Center },
          middle: { anchor: '__container__', align: HorizontalAlign.Center }
        })

      Text("名字是:"+this.params['my_name']+"\n年龄:"+this.params['my_age'])
        .id('name')


      Button("不带参数跳转")
        .onClick(()=>{
          router.pushUrl({
            url: 'pages/MyDivPage'
          })
        })

      Button("返回上一个界面")
        .onClick(()=>{
          router.back()
        })

    }
    .height('100%')
    .width('100%')
  }
}

7.第三个界面代码完善

TypeScript 复制代码
import { router } from '@kit.ArkUI'

@Entry   //页面入口标记
@Component
struct MyDivPage {
  build() {
    Column(){
      Text("这是我手动创建的页面")
      .id('text')
      .fontSize(50)
      .fontWeight(FontWeight.Bold)
      .alignRules({
        center: { anchor: '__container__', align: VerticalAlign.Center },
        middle: { anchor: '__container__', align: HorizontalAlign.Center }
      })


      Button("返回上一个界面")
        .onClick(()=>{
          router.back()
        })
    }
      .width('100%')
      .height('100%')



  }
}

8.效果

案例:使用want启动Ability

1.创建一个新的项目

2.创建第二个界面

page ===> 鼠标右键===>new Page ===> Empty Page ===>在新的界面填写信息===》finsh

页面内容

TypeScript 复制代码
@Entry
@Component
struct MySecond {
  @State message: string = 'Hello World';

  build() {
    RelativeContainer() {
      Text(this.message)
        .id('MySecondHelloWorld')
        .fontSize(50)
        .fontWeight(FontWeight.Bold)
        .alignRules({
          center: { anchor: '__container__', align: VerticalAlign.Center },
          middle: { anchor: '__container__', align: HorizontalAlign.Center }
        })
    }
    .height('100%')
    .width('100%')
  }
}

3.创建一个Ability

ets ==> 鼠标右键 ===> new ===> Ability ==>鼠标点击

填写信息 ===> finsh

4.修改新生成的Ability中的信息

按照图修改即可

修改之后

5.编写Index.ets中的代码

//导入要使用的组件

import { common, Want } from '@kit.AbilityKit';

//获取上下文

private context = getContext(this) as common.UIAbilityContext;

TypeScript 复制代码
import { common, Want } from '@kit.AbilityKit';


@Entry
@Component
struct Index {
  @State message: string = '第一个页面';
  private context = getContext(this) as common.UIAbilityContext;
  build() {
    Column() {
      Text(this.message)
        .id('HelloWorld')
        .fontSize(50)
        .fontWeight(FontWeight.Bold)
        .alignRules({
          center: { anchor: '__container__', align: VerticalAlign.Center },
          middle: { anchor: '__container__', align: HorizontalAlign.Center }
        })

      Button("显式跳转")
        .onClick(() => {

          const want: Want = {
            bundleName: 'com.xiji.myapplication',
            abilityName: 'MySecond'
          }
         this.context.startAbility(want,()=>{
           console.log("跳转成功")
         })


        })
      // 启动按钮

    }
    .height('100%')
    .width('100%')

    //函数

  }


}

下面这张图搞懂BundleName和AbilityName

6.效果图

7.编写隐式启动代码

1)编写module.json5文件

找到module.json5中我们要启动的ability中的这一项,并且一下内容

复制代码
"skills": [{
  "actions": [
    "mySecond"
  ]
}],

2)编写index.ets中的内容

TypeScript 复制代码
import { common, Want, wantConstant } from '@kit.AbilityKit';


@Entry
@Component
struct Index {
  @State message: string = '第一个页面';
  private context = getContext(this) as common.UIAbilityContext;
  build() {
    Column() {
      Text(this.message)
        .id('HelloWorld')
        .fontSize(50)
        .fontWeight(FontWeight.Bold)
        .alignRules({
          center: { anchor: '__container__', align: VerticalAlign.Center },
          middle: { anchor: '__container__', align: HorizontalAlign.Center }
        })

      Button("显式跳转")
        .onClick(() => {

          const want: Want = {
            bundleName: 'com.xiji.myapplication',

            abilityName: 'MySecond'
          }
         this.context.startAbility(want,()=>{
           console.log("跳转成功")
         })


        })
      // 启动按钮

      Button("隐式跳转")
        .onClick(() => {

          const want: Want = {
            action: "mySecond"

          }
          this.context.startAbility(want,()=>{
            console.log("跳转成功")
          })


        })

    }
    .height('100%')
    .width('100%')

    //函数

  }


}

8.隐式启动效果图

相关推荐
LaughingZhu4 小时前
Product Hunt 每日热榜 | 2026-05-21
前端·人工智能·经验分享·chatgpt·html
怕浪猫4 小时前
Electron 开发实战(一):从零入门核心基础与环境搭建
前端·electron·ai编程
小鹏linux5 小时前
Ubuntu 22.04 部署开源免费具有精美现代web页面的Casdoor账号管理系统
linux·前端·ubuntu·开源·堡垒机
前端不太难5 小时前
从单页面到系统化:鸿蒙 App 演进路径
华为·状态模式·harmonyos
前端若水6 小时前
会话管理:创建、切换、删除对话历史
前端·人工智能·python·react.js
Bigger6 小时前
mini-cc:一个轻量级 AI 编程助手的诞生
前端·ai编程·claude
涵涵(互关)6 小时前
Naive-ui树型选择器只显示根节点
前端·ui·vue
BY组态6 小时前
Ricon组态系统最佳实践:从零开始构建物联网监控平台
前端·物联网·iot·web组态·组态
BY组态6 小时前
Ricon组态系统vs传统组态软件:为什么选择新一代Web组态平台
前端·物联网·iot·web组态·组态
SoaringHeart6 小时前
Flutter进阶:OverlayEntry 插入图层管理器 NOverlayZIndexManager
前端·flutter