当前位置:   article > 正文

鸿蒙OS JSUI开发常用的API_鸿蒙3.0.0.166 api是什么版本

鸿蒙3.0.0.166 api是什么版本

鸿蒙OS JSUI 常用的API

路由

// 路由 
import router from '@system.router';

// 跳转页
router.push ({
   uri: 'pages/detail/detail',
   params:{} // 参数 
});
//  返回上一步
router.back();
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

存储数据

import storage from '@system.storage';

// storage.get
// storage.set
// storage.clear   // 清理全部
// storage.delete
//
storage.xxx({
  key: 'storage_key', // get | set | delete
  default :'',   // get 
  value :'',     // set
  success: function(data) {
    console.log('call storage.get success: ' + data);
  },
  fail: function(data, code) {
    console.log('call storage.get fail, code: ' + code + ', data: ' + data);
  },
  complete: function() {
    console.log('call complete');
  },
});
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

弹窗

import prompt from '@system.prompt';

// 文本提示
prompt.showToast({
  message: 'Message Info',  // 展示内容
  duration: 2000,           // 显示时间 默认 1.5 秒
  bottom: 100               // 弹框距离屏幕底部距离
});

// 对话框
prompt.showDialog({
  title: 'Title Info',     // 标题
  message: 'Message Info', // 文本
  buttons: [
    {
      text: 'button',    // 按钮文本
      color: '#666666',  // 按钮颜色
    },
  ],
  success: function(data) {   // 成功 data.index 按钮索引
    console.log('dialog success callback,click button : ' + data.index);
  },
  cancel: function() { // 取消
    console.log('dialog cancel callback');
  },
});
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26

http请求

需要在config.json 声明 ohos.permission.INTERNET 权限

import request from '@system.request';
import fetch from '@system.fetch';

fetch.fetch({
    url: 'http://www.baidu.com' , // 请求地址
    method:'', 					  // 请求方式 GET|OPTIONS|HEAD|POST|PUT|DELETE|TRACE
    data:{}, 				      // 请求数据  string | object
    responseType:'json'			  // 返回类型  text| json 
    success: function(response) {
      console.info("fetch success");
      const { code , data }= JSON.stringify(response); // code 请求状态 200|500|404 , data , headers
    },
    fail: function() {
      console.info("fetch fail");
    }
});
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

开启 http 支持 修改 config.json 中的 usesCleartext:true

{
  "deviceConfig": {
    "default": {
      "network": {
        "usesCleartext": true
      }
      ...
    }
  }
  ...
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

系统通知

import notification from '@system.notification';

notification.show({
  contentTitle: 'title info',    // 标题
  contentText: 'text',			 // 通知内容
  clickAction: {
    bundleName: 'com.huawei.testapp',  // 应用的bundleName
    abilityName: 'notificationDemo',   // 应用的abilityName
    uri: '/path/to/notification',      // 跳转地址
  },
});
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

定时器

// 无需引用 
// setTimeout
// clearTimeout
// setInterval
// clearInterval
  • 1
  • 2
  • 3
  • 4
  • 5

获取设备信息

import device from '@system.device';

device.getInfo({
  success: function(data) {
	const {
		brand ,         // 品牌
		manufacturer,   // 生产商
		model,          // 型号
		product,        // 代号
		language,       // 系统语言
		region,         // 系统地区
		windowWidth,    //可使用的窗口宽度
		windowHeight,   //可使用的窗口高度
		screenDensity,  //屏幕密度
		screenShape,    // 可取值:rect:方形屏;circle:圆形屏
		apiVersion ,    //系统API版本号 
		releaseType,    //
		deviceType      // 设备类型  tablet|phone|tv|wearable|liteWearable|smartVision

	} = data
    console.log('Device information obtained successfully. Device brand:' + data.brand);
  },
  fail: function(data, code) {
    console.log('Failed to obtain device information. Error code:'+ code + '; Error information: ' + data);
  },
});
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26

其他一些api

https://developer.harmonyos.com/cn/docs/documentation/doc-references/js-apis-general-rules-0000000000611789

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/321939
推荐阅读
相关标签
  

闽ICP备14008679号