machichdigital

IDE-Tools · Foto: Homedust, CC BY 2.0

RegelCursor RulesLizenz: CC0 1.0frei kopierbar

Harmony Arkts

Zuletzt aktualisiert:

⬇ Als Datei laden

⧉ –× kopiert⬇ –× heruntergeladenBewertung:

Typ

Regel

Lizenz

CC0 1.0

Anwendungsfeld

Cursor Rules

Voraussetzungen

Keine besonderen — direkt loslegen.

Cursor-Regel für ArkTS auf HarmonyOS, hält Code innerhalb der Sprachgrenzen von ArkTS.

Original-Beschreibung der Autoren: HarmonyOS ArkTS rules for components, state, resources, layout, lifecycle, and accessibility

Die Regel

---
description: HarmonyOS ArkTS rules for components, state, resources, layout, lifecycle, and accessibility
globs: ["**/*.ets", "**/*.ts", "**/*.json5", "AppScope/**/*", "entry/src/main/**/*"]
alwaysApply: false
---

# HarmonyOS ArkTS Rules

## Component Structure

- Use `@Component` for component definitions and PascalCase for component structs.
- Keep state declarations near the top of the component.
- Group lifecycle hooks before `build()`.
- Place `build()` last and keep it focused on UI composition.
- Extract complex UI into smaller components.

## State and Data Flow

- Use `@State` for component-owned state.
- Use `@Prop` for parent-to-child data.
- Use `@Link` only for intentional two-way binding.
- Keep derived values in methods or computed helpers rather than duplicating state.
- Avoid broad global state unless the project has an established app-state pattern.

## Layout and Styling

- Use `Column`, `Row`, `Stack`, `List`, and other ArkUI primitives intentionally.
- Keep layout properties such as width, height, alignment, and layout weight grouped before visual properties.
- Use object notation for margin and padding when sides differ.
- Use logical pixels consistently.
- Use percentage strings for relative sizes.
- Keep reusable spacing, colors, and typography in resources when the project supports it.

## Events and Lifecycle

- Use arrow functions for event handlers.
- Keep event handlers short and delegate complex logic to methods.
- Handle async failures explicitly and surface user-facing errors where appropriate.
- Use lifecycle hooks for setup and teardown that genuinely depends on component lifecycle.

## Resources and Accessibility

- Use `$r()` for app resources.
- Group resource references consistently.
- Add descriptive labels and focus handling for interactive elements.
- Maintain color contrast and touch target size.
- Test on representative device sizes and orientations.

## Common Mistakes

- Do not bury business logic in `build()`.
- Do not use two-way binding when one-way props are enough.
- Do not hardcode repeated strings, colors, and dimensions that belong in resources.
- Do not leave debug `console.log` calls in production code.

So nutzt du sie

Die Regel kopieren (Button oben) oder als Datei herunterladen und im Projekt unter .cursor/rules/ ablegen — Cursor lädt sie beim nächsten Start automatisch. Ältere Cursor-Versionen lesen alternativ eine einzelne .cursorrules-Datei im Projektstamm; dort einfach den Regel-Text ohne den Kopfblock zwischen den ----Zeilen einfügen.

Der Regel-Text ist englisch — Cursor versteht ihn unabhängig von der Sprache, in der Sie mit dem Editor chatten.

Im Detail

ArkTS ist eine von TypeScript abgeleitete, aber eingeschränkte Sprache für HarmonyOS-Apps, Huaweis Android-Alternative – viele dynamische JavaScript- und TypeScript-Features sind dort nicht erlaubt. Diese Regel bringt Cursor bei, sich an diese Einschränkungen zu halten, statt versehentlich Standard-TypeScript-Code zu generieren, der auf HarmonyOS nicht kompiliert. Relevant für Entwickler, die Apps für den HarmonyOS- bzw. chinesischen Markt bauen, wo Huawei-Geräte ohne Google-Dienste laufen. Ohne diese Regel produziert Cursor sonst häufig Code mit nicht unterstützten Sprachkonstrukten, der manuell nachgebessert werden muss. Für Android- oder iOS-Projekte ohne HarmonyOS-Ziel nicht relevant.

Praxis-Tipp

Lassen Sie sich damit z. B. eine einfache ArkUI-Komponente generieren – die Regel verhindert typische Stolperfallen wie unerlaubte any-Typen oder dynamische Objekterweiterung.

Siehe auch

Lizenz & Quelle

Inhalt ansehen (harmony-arkts.mdc)
Lade …

Erfahrungen & Kommentare.

Funktioniert der Regel bei Ihnen? Tipps, Stolperfallen, Varianten — teilen Sie es mit der Community.

Lade Kommentare …

Ihre IP-Adresse wird zum Schutz vor Missbrauch gespeichert und nach 14 Tagen automatisch entfernt (Datenschutz).

Passt dazu.