
1. 鸿蒙HarmonyOS NEXT开发环境搭建1.1 DevEco Studio安装配置开发鸿蒙应用首先需要安装DevEco Studio这是华为官方提供的集成开发环境。最新版本已支持HarmonyOS NEXT星河版开发建议从官网下载4.1及以上版本。安装时注意勾选ArkTS语言支持包和鸿蒙模拟器组件。安装完成后需要进行基础配置设置HarmonyOS SDK路径建议使用默认路径下载Platform 9和Tools最新版本配置Node.js环境内置版本即可安装Gradle 7.5版本注意首次启动时如果模拟器一直加载可以尝试以下方案检查BIOS中是否开启虚拟化支持关闭Windows Hyper-V功能以管理员身份运行Studio1.2 创建首个ArkTS项目新建项目时选择Application - Empty Ability模板选择ArkTS。项目结构主要包含entry/src/main/etsArkTS源码目录entry/src/main/resources资源文件目录oh-package.json5依赖管理文件关键配置文件说明// module.json5 { module: { name: entry, type: entry, abilities: [ { name: MainAbility, srcEntry: ./ets/MainAbility/MainAbility.ts, icon: $media:icon, label: $string:MainAbility_label } ] } }2. ArkTS面向对象开发实践2.1 ArkTS语言特性ArkTS是TypeScript的超集在保留TS特性的基础上增加了鸿蒙特有的能力支持声明式UI描述内置状态管理机制强化类型系统如Observed装饰器组件生命周期钩子基础类型示例// 基本类型 let name: string HarmonyOS let version: number 4.0 let isReleased: boolean true // 数组和元组 let features: string[] [ArkTS, eTS] let versionInfo: [string, number] [NEXT, 2024] // 枚举 enum DeviceType { PHONE 1, TABLET, TV, WEARABLE }2.2 类与对象实现ArkTS完全支持面向对象特性包括类定义与继承接口实现访问修饰符public/private/protected抽象类与方法组件基类示例abstract class BaseComponent { protected componentName: string constructor(name: string) { this.componentName name } abstract render(): void printInfo(): void { console.log(Component: ${this.componentName}) } } class MyButton extends BaseComponent { private label: string constructor(label: string) { super(Button) this.label label } render(): void { // 实现渲染逻辑 } }2.3 常用设计模式应用单例模式设备管理class DeviceManager { private static instance: DeviceManager private devices: string[] [] private constructor() {} public static getInstance(): DeviceManager { if (!DeviceManager.instance) { DeviceManager.instance new DeviceManager() } return DeviceManager.instance } addDevice(device: string): void { this.devices.push(device) } }观察者模式状态管理interface Observer { update(data: any): void } class Subject { private observers: Observer[] [] attach(observer: Observer): void { this.observers.push(observer) } notify(data: any): void { this.observers.forEach(obs obs.update(data)) } } class UserObserver implements Observer { update(data: any): void { console.log(User data changed:, data) } }3. 组件化UI开发实战3.1 基础组件使用鸿蒙提供丰富的内置组件主要分为布局组件Row、Column、Stack等基础组件Text、Image、Button等容器组件List、Grid、Swiper等计数器组件示例Entry Component struct CounterComponent { State count: number 0 build() { Column() { Text(Count: ${this.count}) .fontSize(30) Button(1) .onClick(() { this.count }) } .width(100%) .height(100%) .justifyContent(FlexAlign.Center) } }3.2 自定义组件开发卡片组件封装Component export struct ProductCard { private title: string private price: number State isFavorite: boolean false build() { Column() { Image($r(app.media.product)) .width(120) .height(120) Text(this.title) .fontSize(16) Row() { Text(¥${this.price}) .fontColor(#FF0000) Image(this.isFavorite ? $r(app.media.favorite) : $r(app.media.unfavorite)) .onClick(() { this.isFavorite !this.isFavorite }) } } .padding(10) .borderRadius(8) .backgroundColor(#FFFFFF) } }组件参数传递Entry Component struct ParentComponent { private product { title: HarmonyOS开发指南, price: 99 } build() { Column() { ProductCard({ title: this.product.title, price: this.product.price }) } } }3.3 状态管理与数据绑定State基本用法Entry Component struct StateExample { State message: string Hello State count: number 0 build() { Column() { Text(this.message) Text(Count: ${this.count}) Button(Change) .onClick(() { this.message Hello HarmonyOS this.count }) } } }Link双向绑定Component struct ChildComponent { Link value: number build() { Button(1) .onClick(() this.value) } } Entry Component struct ParentComponent { State count: number 0 build() { Column() { Text(Parent: ${this.count}) ChildComponent({ value: $count }) } } }4. 综合应用实例电商APP开发4.1 项目结构设计ecommerce/ ├── entry/ │ ├── src/ │ │ ├── main/ │ │ │ ├── ets/ │ │ │ │ ├── components/ # 公共组件 │ │ │ │ ├── model/ # 数据模型 │ │ │ │ ├── pages/ # 页面组件 │ │ │ │ ├── services/ # 服务层 │ │ │ │ └── utils/ # 工具类 │ │ │ └── resources/ # 资源文件 │ └── oh-package.json5 # 依赖管理4.2 商品列表页实现数据模型定义// model/Product.ts export class Product { constructor( public id: string, public name: string, public price: number, public image: Resource, public description?: string ) {} }列表页面// pages/ProductList.ets Entry Component struct ProductListPage { private products: Product[] [ new Product(1, HarmonyOS手机, 3999, $r(app.media.phone)), new Product(2, 智能手表, 1299, $r(app.media.watch)) ] build() { List({ space: 10 }) { ForEach(this.products, (item: Product) { ListItem() { ProductCard({ title: item.name, price: item.price }) } }) } .padding(10) } }4.3 购物车功能实现购物车服务// services/CartService.ts export class CartService { private items: Product[] [] addToCart(product: Product): void { this.items.push(product) } getCartItems(): Product[] { return this.items } getTotal(): number { return this.items.reduce((sum, item) sum item.price, 0) } }购物车页面// pages/CartPage.ets Entry Component struct CartPage { private cartService: CartService new CartService() build() { Column() { List() { ForEach(this.cartService.getCartItems(), (item) { ListItem() { Text(item.name) Text(¥${item.price}) } }) } Text(总计: ¥${this.cartService.getTotal()}) .fontSize(20) .fontColor(#FF0000) } } }5. 调试与性能优化5.1 常见问题排查页面空白问题检查组件build方法是否有返回值确认资源路径是否正确使用$r()引用查看日志是否有异常抛出样式不生效检查样式属性是否支持鸿蒙有部分限制确认单位使用正确vp/fp/px尝试添加!important标记数据绑定失效确保使用State/Link装饰器检查对象引用是否变化需要创建新对象触发更新复杂对象建议使用Observed装饰器5.2 性能优化建议列表优化为ListItem设置固定高度使用LazyForEach替代ForEach处理大数据量避免在列表项中使用复杂计算渲染优化减少不必要的状态更新使用Prop替代Link当不需要双向绑定时复杂界面考虑使用条件渲染内存管理及时取消事件监听大图片使用合适的分辨率避免在全局保存大量数据实际开发中发现合理使用State和Link能显著提升性能。对于不会变化的属性直接使用常规属性而非响应式属性可以减少不必要的渲染。