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

实际效果:

相关推荐
AlbertZein3 小时前
HarmonyOS一杯冰美式的时间 -- @Env
harmonyos
以太浮标8 小时前
华为eNSP模拟器综合实验之-BFD联动配置解析
运维·网络·华为·信息与通信
小雨青年9 小时前
鸿蒙 HarmonyOS 6 | ArkUI (05):布局进阶 RelativeContainer 相对布局与 Flex 弹性布局
华为·harmonyos
特立独行的猫a12 小时前
鸿蒙PC三方库编译libiconv链接报错,解决 libtool 链接参数丢失问题过程总结
harmonyos·交叉编译·libiconv·三方库·鸿蒙pc·libtool
哈__13 小时前
Flutter 开发鸿蒙 PC 第一个应用:窗口创建 + 大屏布局
flutter·华为·harmonyos
特立独行的猫a13 小时前
鸿蒙PC命令行及三方库libiconv移植:鸿蒙PC生态的字符编码基石
harmonyos·交叉编译·libiconv·三方库移植·鸿蒙pc
以太浮标16 小时前
华为eNSP模拟器综合实验之- PPP协议解析及配置案例
运维·网络·华为·信息与通信
不爱学英文的码字机器16 小时前
【鸿蒙PC命令行适配】基于OHOS SDK直接构建xz命令集(xz、xzgrep、xzdiff),完善tar.xz解压能力
华为·harmonyos
特立独行的猫a17 小时前
[鸿蒙PC命令行程序移植实战]:交叉编译移植最新openSSL 4.0.0到鸿蒙PC
华为·harmonyos·移植·openssl·交叉编译·鸿蒙pc
特立独行的猫a19 小时前
[鸿蒙PC命令行适配] 移植Aria2文件下载神器最新版到鸿蒙PC的完整教程 (附可运行程序)
harmonyos·移植·交叉编译·aria2·鸿蒙pc