鸿蒙 DevEcoStudio:简单实现网络请求登录案例

使用http或axios实现登录案例

在entry/src/main/ets/pages路径下新建Page9.ets文件:

复制代码
import http from '@ohos.net.http'
import router from '@ohos.router'
@Entry
@Component
struct Page9 {
  @State message: string = 'Hello World'
  @State username: string = ''
  @State password: string = ''
  build() {
    Row() {
      Column() {
        TextInput({placeholder:"请输入用户名"})
          .onChange(value=>{
            this.username=value
          })
        TextInput({placeholder:"请输入密码"})
          .onChange(value=>{
            this.password=value
          })
        Button('登录')
          .onClick(()=>{
            //创建http请求对象
            let httpRequest=http.createHttp()
            //发送请求
            httpRequest.request(
              //接口
              'https://2abe08c9-2ea0-46d7-927e-05885b0eea12.mock.pstmn.io/abcd',
              {
                method:http.RequestMethod.GET
              }
            )
              .then((resp:http.HttpResponse)=>{
                if (JSON.parse(resp.result.toString()).username==this.username && JSON.parse(resp.result.toString()).pwd==this.password) {
                  router.pushUrl({url:'page/Page10'})
                }
              })
          })
      }
      .width('100%')
    }
    .height('100%')
  }
}

修改entry/src/main/module.json5文件:

复制代码
{
  "module": {
    "name": "entry",
    "requestPermissions": [
      {
        "name": "ohos.permission.INTERNET",
      }
    ],
    "type": "entry",
    "description": "$string:module_desc",
    "mainElement": "EntryAbility",
    "deviceTypes": [
      "phone",
      "tablet"
    ],
    "deliveryWithInstall": true,
    "installationFree": false,
    "pages": "$profile:main_pages",
    "abilities": [
      {
        "name": "EntryAbility",
        "srcEntry": "./ets/entryability/EntryAbility.ts",
        "description": "$string:EntryAbility_desc",
        "icon": "$media:icon",
        "label": "$string:EntryAbility_label",
        "startWindowIcon": "$media:icon",
        "startWindowBackground": "$color:start_window_background",
        "exported": true,
        "skills": [
          {
            "entities": [
              "entity.system.home"
            ],
            "actions": [
              "action.system.home"
            ]
          }
        ]
      }
    ]
  }
}

在entry/src/main/ets/pages路径下新建Page10.ets文件,作为登录成功后的页面:

复制代码
@Entry
@Component
struct Page10 {
  @State message: string = '跳转成功'

  build() {
    Row() {
      Column() {
        Text(this.message)
          .fontSize(50)
          .fontWeight(FontWeight.Bold)
      }
      .width('100%')
    }
    .height('100%')
  }
}

实际效果:

相关推荐
深海的鲸同学 luvi6 小时前
【HarmonyOS】个性化应用图标动态切换详解
华为·harmonyos
奔跑的露西ly8 小时前
【HarmonyOS NEXT】ohpm 安装依赖失败(@finclip 包找不到)问题复盘与解决方案
华为·harmonyos
余生H8 小时前
时光小铺鸿蒙商城上架全复盘 - 鸿蒙2025领航者闯关.成长升级路
华为·harmonyos·鸿蒙2025领航者闯关
鸭蛋超人不会飞9 小时前
鸿蒙OS学习与项目搭建报告
harmonyos
LRX_1989279 小时前
华为设备配置练习(七)VRRP 配置
服务器·网络·华为
waeng_luo10 小时前
[鸿蒙2025领航者闯关]图标资源统一管理
harmonyos·鸿蒙2025领航者闯关·鸿蒙6实战·开发者年度总结
挨踢攻城10 小时前
华为项目管理的43210法则
华为·项目管理·信息系统项目管理师·pmp·软考高项·华为项目管理·公众号厦门微思网络
云上漫步者11 小时前
深度实战:Rust交叉编译适配OpenHarmony PC——unicode_width完整适配案例
开发语言·后端·rust·harmonyos
yumgpkpm12 小时前
Iceberg在Hadoop集群使用步骤(适配AI大模型)
大数据·hadoop·分布式·华为·zookeeper·开源·cloudera
遇到困难睡大觉哈哈12 小时前
Harmony OS Web 组件:如何在新窗口中打开网页(实战分享)
前端·华为·harmonyos