HarmonyOS开发基础

1作者: flfljh大约 1 个月前原帖
# HarmonyOS开发基础 ## 数据存储 HarmonyOS应用程序提供多种数据存储选项: - *首选项(Preferences)*:用于简单数据的轻量级键值存储 - *SQLite*:用于结构化数据存储的关系型数据库 - *文件存储*:直接访问文件系统以进行自定义数据存储 ## 数组 数组是按索引访问的有序数据集合: ```javascript let numbers = [1, 2, 3, 4, 5]; console.log(numbers[0]); // 输出 1 ``` ## 函数 ### 常规函数 封装可重用的代码块: ```javascript function greet(name) { return "Hello, " + name; } console.log(greet("Alice")); // 输出 "Hello, Alice" ``` ### 箭头函数 简洁的函数语法: ```javascript const greet = (name) => "Hello, " + name; console.log(greet("Bob")); // 输出 "Hello, Bob" ``` ## 接口 定义对象结构的契约: ```typescript interface Person { name: string; age: number; greet(): string; } class Employee implements Person { name: string; age: number; constructor(name: string, age: number) { this.name = name; this.age = age; } greet(): string { return "Hello, I am " + this.name; } } ``` ## 对象和方法 属性和方法的集合: ```javascript let person = { name: "Charlie", age: 30, greet: function() { return "Hello, " + this.name; } }; console.log(person.greet()); // 输出 "Hello, Charlie" ``` ## 联合类型 可以是多种类型之一的值: ```typescript let age: number | string; age = 25; // 有效 age = "twenty-five"; // 也有效 ``` ## 枚举 命名常量集合: ```typescript enum Direction { Up, Down, Left, Right } let direction: Direction = Direction.Up; // 使用枚举值 ``` ------ ## UI开发概念与布局 ### 组件属性和方法 - *属性*:配置外观和行为 - *方法*:执行特定于组件的操作 ### 文本样式 - *文本颜色*:通过 `text-color` 属性设置 - *文本溢出*:通过 `text-overflow` 控制(例如,`ellipsis` 显示截断点) ### 图像组件 ```html <image src="path/to/image.png" width="100" height="100"></image> ``` ### 输入字段 ```html <input type="text" placeholder="Enter text here"></input> ``` ### SVG图标 直接嵌入或引用SVG文件: ```html <svg width="100" height="100"> <circle cx="50" cy="50" r="40" stroke="black" stroke-width="3" fill="red" /> </svg> ``` ### 布局元素 组织UI组件的位置和大小 #### 边距 ```css .container { margin: 10px; /* 统一边距 */ margin: 5px 10px; /* 垂直 | 水平 */ margin: 5px 10px 15px; /* 上 | 水平 | 下 */ margin: 5px 10px 15px 20px; /* 上 | 右 | 下 | 左 */ } ``` #### 边框 ```css .box { border: 1px solid black; /* 宽度 | 样式 | 颜色 */ border-radius: 10px; /* 圆角 */ } ``` #### 自定义形状 ```css .special-shape { border-radius: 20px 10px 30px 5px; /* 自定义圆角 */ } ``` ### 背景 ```css .background { background-color: #f0f0f0; /* 纯色 */ background-image: url('path/to/image.png'); /* 背景图像 */ background-position: center; /* 图像定位 */ background-size: cover; /* 图像大小 */ background-repeat: no-repeat; /* 防止平铺 */ } ``` ### 线性布局对齐 ```css .linear-layout { display: flex; justify-content: center; /* 主轴对齐 */ align-items: center; /* 交叉轴对齐 */ flex-direction: row; /* 布局方向(行/列) */ } ```
查看原文
# Fundamentals of HarmonyOS Development<p>## Data Storage<p>HarmonyOS applications offer multiple data storage options:<p>- *Preferences*: Lightweight key-value storage for simple data - *SQLite*: Relational database for structured data storage - *File Storage*: Direct file system access for custom data storage<p>## Arrays<p>Arrays are ordered collections of data accessible by index:<p>```javascript let numbers = [1, 2, 3, 4, 5]; console.log(numbers[0]); &#x2F;&#x2F; Outputs 1 ```<p>## Functions<p>### Regular Functions Encapsulate reusable blocks of code: ```javascript function greet(name) { return &quot;Hello, &quot; + name; } console.log(greet(&quot;Alice&quot;)); &#x2F;&#x2F; Outputs &quot;Hello, Alice&quot; ```<p>### Arrow Functions Concise function syntax: ```javascript const greet = (name) =&gt; &quot;Hello, &quot; + name; console.log(greet(&quot;Bob&quot;)); &#x2F;&#x2F; Outputs &quot;Hello, Bob&quot; ```<p>## Interfaces<p>Define object structure contracts: ```typescript interface Person { name: string; age: number; greet(): string; }<p>class Employee implements Person { name: string; age: number;<p><pre><code> constructor(name: string, age: number) { this.name = name; this.age = age; } greet(): string { return &quot;Hello, I am &quot; + this.name; } } ``` </code></pre> ## Objects and Methods<p>Collections of properties and methods: ```javascript let person = { name: &quot;Charlie&quot;, age: 30, greet: function() { return &quot;Hello, &quot; + this.name; } }; console.log(person.greet()); &#x2F;&#x2F; Outputs &quot;Hello, Charlie&quot; ```<p>## Union Types<p>Values that can be one of several types: ```typescript let age: number | string; age = 25; &#x2F;&#x2F; Valid age = &quot;twenty-five&quot;; &#x2F;&#x2F; Also valid ```<p>## Enums<p>Named constant collections: ```typescript enum Direction { Up, Down, Left, Right } let direction: Direction = Direction.Up; &#x2F;&#x2F; Uses enum value ```<p>------<p>## UI Development Concepts and Layout<p>### Component Properties and Methods - *Properties*: Configure appearance and behavior - *Methods*: Perform component-specific operations<p>### Text Styling - *Text Color*: Set via `text-color` property - *Text Overflow*: Control with `text-overflow` (e.g., `ellipsis` displays truncation dots)<p>### Image Component ```html &lt;image src=&quot;path&#x2F;to&#x2F;image.png&quot; width=&quot;100&quot; height=&quot;100&quot;&gt;&lt;&#x2F;image&gt; ```<p>### Input Field ```html &lt;input type=&quot;text&quot; placeholder=&quot;Enter text here&quot;&gt;&lt;&#x2F;input&gt; ```<p>### SVG Icons Embed directly or reference SVG files: ```html &lt;svg width=&quot;100&quot; height=&quot;100&quot;&gt; &lt;circle cx=&quot;50&quot; cy=&quot;50&quot; r=&quot;40&quot; stroke=&quot;black&quot; stroke-width=&quot;3&quot; fill=&quot;red&quot; &#x2F;&gt; &lt;&#x2F;svg&gt; ```<p>### Layout Elements Organize UI component positioning and sizing<p>#### Margin ```css .container { margin: 10px; &#x2F;* Uniform margin <i>&#x2F; margin: 5px 10px; &#x2F;</i> Vertical | Horizontal <i>&#x2F; margin: 5px 10px 15px; &#x2F;</i> Top | Horizontal | Bottom <i>&#x2F; margin: 5px 10px 15px 20px; &#x2F;</i> Top | Right | Bottom | Left <i>&#x2F; } ```<p>#### Borders ```css .box { border: 1px solid black; &#x2F;</i> Width | Style | Color <i>&#x2F; border-radius: 10px; &#x2F;</i> Rounded corners <i>&#x2F; } ```<p>#### Custom Shapes ```css .special-shape { border-radius: 20px 10px 30px 5px; &#x2F;</i> Custom rounded corners <i>&#x2F; } ```<p>### Backgrounds ```css .background { background-color: #f0f0f0; &#x2F;</i> Solid color <i>&#x2F; background-image: url(&#x27;path&#x2F;to&#x2F;image.png&#x27;); &#x2F;</i> Background image <i>&#x2F; background-position: center; &#x2F;</i> Image positioning <i>&#x2F; background-size: cover; &#x2F;</i> Image sizing <i>&#x2F; background-repeat: no-repeat; &#x2F;</i> Prevent tiling <i>&#x2F; } ```<p>### Linear Layout Alignment ```css .linear-layout { display: flex; justify-content: center; &#x2F;</i> Main axis alignment <i>&#x2F; align-items: center; &#x2F;</i> Cross axis alignment <i>&#x2F; flex-direction: row; &#x2F;</i> Layout direction (row&#x2F;column) *&#x2F; } ```