鸿蒙系统开发

有用的网站

鸿蒙官网:https://www.harmonyos.com/
鸿蒙系统开发者:https://developer.harmonyos.com/
华为开发者:https://developer.huawei.com/
在线体验:https://playground.harmonyos.com/
Gitee:https://gitee.com/openharmony
JS API:https://developer.harmonyos.com/cn/docs/documentation/doc-references/js-apis-overview-0000001056361791

开发环境搭建 (DevEco Studio)

安装模拟器

  • 安装菜单
    • Tools -> Device Manager
  • 注册华为开发者

模拟器(Simulator)与预览器(Previewer)的区别:

  1. 预览器支持热更新,模拟器不支持热更新
    2.预览器中不能直接返回接口数据,模拟器可以返回接口数据

汉化菜单

  • 点选菜单 File -> Settings,
  • 然后点选 Plugins -> Marketplate,
  • 然后搜索 Chinese,
  • 然后选择 Chinese(Simplified)Language Pack / 中文语言包。
  • 然后点击 install 执行安装
  • 安装完成后重启 IDE

JS UI 框架

详情查看:https://developer.harmonyos.com/cn/documentation

目录结构

配置文件(config.json)

生命周期

应用生命周期

  • onCreate:应用启动时调用
  • onDestroy:应用销毁时调用

⻚面生命周期

路由与导航

详情查看:https://developer.harmonyos.com/cn/docs/documentation/doc-references/js-apis-basic-features-routes-0000000000611824

声明路由

在 config.json 中声明路由

{
  // ...
  "module": {
    "js": [
      {
        "pages": [
          "pages/index/index",
          "pages/news/news",
          "pages/profile/index",
        ],
        "name": "default",
        "window": {
          "designWidth": 720,
          "autoDesignWidth": true
        }
      }
    ]
  }
}

在 pages 目录下声明对应的三个文件

声明导航

引入 router

import router from '@system.router';

声明导航方法

export default {
	// ...
	
	launch: function(){
		router.push({
			uri:'pages/details/details',
		});
	}
}

JS语法

详情查看:https://developer.harmonyos.com/cn/docs/documentation/doc-references/js-framework-syntax-js-0000000000611432
支持 ES 6 语法(但不支持最新的 ES 语法)
鸿蒙 JS 是参考 Vue 2 封装的

JS应用

$def

  • 在⻚面中,通过 this. a p p . app. app.def,获取在 app.js 中暴露的对象

数据绑定

  • data | public:类型是对象或者函数
  • private:数据只能由当前⻚面修改

数据修改

  • this.$set(‘key’, value);
  • this.$delete(‘key’);

获取 DOM 元素

  • $refs

    // index.hml
    <text ref="target">内容</div>
    
    // index.js
    const t = this.$refs.target; // 获取 ref 属性为 target 的 DOM 元素
    
  • $element

    // index.hml
    <text id="target">内容</div>
    
    // index.js
    const t = this.$element("target"); // 获取 id 属性为 target 的 DOM 元素
    
    // 获取根组件对象
    const t = this.$element();
    

JS架构

JS UI 框架

JS 应用开发框架

JS 原生模块(NAPI)

HML语法

详情查看:https://developer.harmonyos.com/cn/docs/documentation/doc-references/js-framework-syntax-hml-0000000000611413

CSS语法

详情查看:https://developer.harmonyos.com/cn/docs/documentation/doc-references/js-framework-syntax-css-0000000000611425

多语言支持

详情查看:https://developer.harmonyos.com/cn/docs/documentation/doc-references/js-framework-multiple-languages-0000000000625923

  • i18n 目录下存放语言包
    语言-地区.json(zh-CN.json)
  • $t() 获取对应的内容
  • 切换系统语言时(模拟器或真机中),可以看到效果

组件

在这里插入图片描述
基础组件

button
提供按钮组件,包括胶囊按钮、圆形按钮、文本按钮、弧形按钮、下载按钮。

详情查看:https://developer.harmonyos.com/cn/docs/documentation/doc-references/js-components-basic-button-0000000000621726

容器组件

div

详情查看:https://developer.harmonyos.com/cn/docs/documentation/doc-references/js-service-widget-container-div-0000001106548606

通用组件

详情查看:https://developer.harmonyos.com/cn/docs/documentation/doc-references/js-service-widget-common-attributes-0000001161259599

自定义组件

详情查看:https://developer.harmonyos.com/cn/docs/documentation/doc-references/js-service-widget-custom-basic-usage-0000001115938360

基本用法
自定义组件通过element引入到宿主⻚面

<element name='comp' src='../../common/component/comp.hml'></element>
<div>
	<comp prop1='xxxx'@child1="bindParentVmMethod"></comp>
</div>

插槽

匿名插槽
父组件:<tag><text>内容</text></tag>
子组件:<slot></slot>

具名插槽
父组件:<tag><text slot=“slotname”>内容</text></tag>
子组件:<slot name=“slotname”></slot>

基本功能

详情查看:https://developer.harmonyos.com/cn/docs/documentation/doc-references/js-apis-basic-features-app-context-0000000000611801

网络功能

详情查看:https://developer.harmonyos.com/cn/docs/documentation/doc-references/js-apis-network-data-request-0000000000626077

在这里插入图片描述
在这里插入图片描述

系统功能

通知消息

地理位置

网络状态

设备信息

屏幕亮度

数据存储

官方Demo

鸿蒙提供的一些具体应用实例。有代码,有文字介绍,有效果演示

详情查看:https://developer.harmonyos.com/cn/documentation/codelabs/

JS 购物应用

详情查看:https://developer.huawei.com/consumer/cn/codelabsPortal/carddetails/JS-COMPONENTS

JS 计步器卡片

详情查看:https://developer.huawei.com/consumer/cn/codelabsPortal/carddetails/Step-Card

分布式新闻分享

详情查看:https://developer.huawei.com/consumer/cn/codelabsPortal/carddetails/HarmonyOS-NewsClient

分布式亲自教育

详情查看:https://developer.huawei.com/consumer/cn/codelabsPortal/carddetails/HarmonyOS-EducationSystem

  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值