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

实际效果:

相关推荐
m0_749690231 小时前
【寻迹校园 HarmonyOS NEXT 实战 06】共享权威词表:让发布表单与首页筛选使用同一套分类
harmonyos·arkts·数据建模·软件架构·arkui
独守一片天3 小时前
鸿蒙新生态服务卡片设计与状态同步
华为·harmonyos
m0_749690233 小时前
【寻迹校园 HarmonyOS NEXT 实战 07】不引入全局状态库:用 dataRevision 实现跨页面刷新
harmonyos·arkts·软件架构·状态管理·arkui
贾伟康3 小时前
【中国方言题库|01】HarmonyOS ArkTS 方言题库首页实战:组织地区入口、推荐内容和学习进度
harmonyos·arkts·arkui·多设备适配·本地数据
贾伟康5 小时前
【中国方言题库|04】HarmonyOS ArkTS 粤语分库实战:处理繁简文本、读音与练习入口
harmonyos·arkts·unicode·tts·arkui
OH_TPC5 小时前
HarmonyOS APP开发---"壁纸控"壁纸App,需要用到这个库
harmonyos
独守一片天6 小时前
鸿蒙穿戴设备与健康服务闭环
华为·harmonyos
Magic-ZYJ6 小时前
隐私优先 HarmonyOS 应用怎么设计:无账号、无后台、无统计 SDK
华为·harmonyos·鸿蒙·独立开发者·心晴手记
独守一片天6 小时前
鸿蒙车机手机协同服务生态
华为·智能手机·harmonyos
2501_919749037 小时前
华为鸿蒙测手速APP—小羊手速
华为·harmonyos·鸿蒙