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

实际效果:

相关推荐
不才难以繁此生20 分钟前
HarmonyOS ArkUI NavPathStack 实战:中式美食详情页多入口返回栈怎么设计
harmonyos·arkts·arkui·navigation·中式美食·navpathstack·路由栈
不才难以繁此生1 小时前
ArkTS 本地数据库实战|中式美食购物清单:按菜谱来源合并食材数量
harmonyos·arkts·repository·rdb·中式美食·购物清单·本地数据库
ZZZMMM.zip1 小时前
基于鸿蒙HarmonyOS NEXT开发AI书籍推荐应用:智能阅读新体验与鸿蒙Flutter框架跨端实践
人工智能·flutter·华为·harmonyos·鸿蒙
不才难以繁此生1 小时前
HarmonyOS ResourceStr 实战:中式美食菜谱封面怎么统一注册、兜底和缓存
harmonyos·arkts·image·arkui·中式美食·resourcestr·图片缓存
Helen_cai1 小时前
OpenHarmony http 网络请求封装与全局拦截实战(API Version23 + 适配版)
网络·网络协议·http·华为·harmonyos
Helen_cai5 小时前
OpenHarmony 完整项目工程整合规范 + 模块化分层架构(API23+ 标准企业级结构)
开发语言·华为·php·harmonyos
木木子229 小时前
# 密码强度检测深度解析:正则表达式实时分析、多维度评分算法与可视化反馈
mysql·算法·华为·正则表达式·harmonyos
星释13 小时前
鸿蒙智能体开发实战:6.A2A 模式开发接口
华为·harmonyos·鸿蒙·智能体
澹然疏离13 小时前
HarmonyOS应用《民族图鉴》开发第34篇:TTSEngineService——语音引擎封装与降级策略
华为·harmonyos
花开彼岸天~14 小时前
03 module.json5 配置文件全解:能力声明、权限申请与设备支持
华为·harmonyos·鸿蒙系统