鸿蒙 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%')
  }
}

实际效果:

相关推荐
木斯佳6 小时前
HarmonyOS 6实战::多组件嵌套场景下,自动化测试覆盖复杂交互实践
华为·交互·harmonyos
以太浮标7 小时前
华为eNSP模拟器综合实验之- IS-IS路由协议实践配置解析
网络协议·网络安全·华为·智能路由器·信息与通信
键盘鼓手苏苏11 小时前
Flutter 三方库 pip 的鸿蒙化适配指南 - 实现标准化的画中画(Picture-in-Picture)模式、支持视频悬浮窗与多任务并行交互
flutter·pip·harmonyos
左手厨刀右手茼蒿11 小时前
Flutter 组件 sheety_localization 的适配 鸿蒙Harmony 实战 - 驾驭在线协作式多语言管理、实现鸿蒙端动态词条下发与全球化敏捷发布方案
flutter·harmonyos·鸿蒙·openharmony·sheety_localization
见山是山-见水是水11 小时前
鸿蒙flutter第三方库适配 - 路由书签应用
flutter·华为·harmonyos
火柴就是我11 小时前
记录一些跨平台开发需要的鸿蒙知识
flutter·harmonyos
小雨青年12 小时前
鸿蒙 HarmonyOS 6 | 空间音频技术实战指南
华为·音视频·harmonyos
Huanzhi_Lin13 小时前
鸿蒙NEXT-HelloWorld
华为·harmonyos·arkts·arkui·ets
特立独行的猫a14 小时前
使用 vcpkg 为鸿蒙(HarmonyOS / OHOS)下载与安装三方库实践指南
华为·harmonyos·openharmony·vcpkg·三方库·鸿蒙pc
音视频牛哥14 小时前
HarmonyOS NEXT系统RTSP、RTMP超低延迟直播播放器全面解析
华为·harmonyos·鸿蒙rtmp播放器·鸿蒙rtsp播放器·纯血鸿蒙rtsp播放器·harmonyos rtsp·harmonyos rtmp