HarmonyOS开发基础
# 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]); // Outputs 1
```<p>## Functions<p>### Regular Functions
Encapsulate reusable blocks of code:
```javascript
function greet(name) {
return "Hello, " + name;
}
console.log(greet("Alice")); // Outputs "Hello, Alice"
```<p>### Arrow Functions
Concise function syntax:
```javascript
const greet = (name) => "Hello, " + name;
console.log(greet("Bob")); // Outputs "Hello, Bob"
```<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 "Hello, I am " + this.name;
}
}
```
</code></pre>
## Objects and Methods<p>Collections of properties and methods:
```javascript
let person = {
name: "Charlie",
age: 30,
greet: function() {
return "Hello, " + this.name;
}
};
console.log(person.greet()); // Outputs "Hello, Charlie"
```<p>## Union Types<p>Values that can be one of several types:
```typescript
let age: number | string;
age = 25; // Valid
age = "twenty-five"; // Also valid
```<p>## Enums<p>Named constant collections:
```typescript
enum Direction {
Up,
Down,
Left,
Right
}
let direction: Direction = Direction.Up; // 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
<image src="path/to/image.png" width="100" height="100"></image>
```<p>### Input Field
```html
<input type="text" placeholder="Enter text here"></input>
```<p>### SVG Icons
Embed directly or reference SVG files:
```html
<svg width="100" height="100">
<circle cx="50" cy="50" r="40" stroke="black" stroke-width="3" fill="red" />
</svg>
```<p>### Layout Elements
Organize UI component positioning and sizing<p>#### Margin
```css
.container {
margin: 10px; /* Uniform margin <i>/
margin: 5px 10px; /</i> Vertical | Horizontal <i>/
margin: 5px 10px 15px; /</i> Top | Horizontal | Bottom <i>/
margin: 5px 10px 15px 20px; /</i> Top | Right | Bottom | Left <i>/
}
```<p>#### Borders
```css
.box {
border: 1px solid black; /</i> Width | Style | Color <i>/
border-radius: 10px; /</i> Rounded corners <i>/
}
```<p>#### Custom Shapes
```css
.special-shape {
border-radius: 20px 10px 30px 5px; /</i> Custom rounded corners <i>/
}
```<p>### Backgrounds
```css
.background {
background-color: #f0f0f0; /</i> Solid color <i>/
background-image: url('path/to/image.png'); /</i> Background image <i>/
background-position: center; /</i> Image positioning <i>/
background-size: cover; /</i> Image sizing <i>/
background-repeat: no-repeat; /</i> Prevent tiling <i>/
}
```<p>### Linear Layout Alignment
```css
.linear-layout {
display: flex;
justify-content: center; /</i> Main axis alignment <i>/
align-items: center; /</i> Cross axis alignment <i>/
flex-direction: row; /</i> Layout direction (row/column) */
}
```