# view-model **Repository Path**: Duke_Bit/view-model ## Basic Information - **Project Name**: view-model - **Description**: 搭配lifecycle的ViewModel,在组件销毁时自动调用clean进行清理 - **Primary Language**: Unknown - **License**: MIT - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 1 - **Forks**: 1 - **Created**: 2025-09-04 - **Last Updated**: 2025-09-04 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # ViewModel ## 简介 [![openHarmony](https://img.shields.io/badge/openharmony-v1.0.0-brightgreen)](https://gitee.com/Duke_Bit/view-model/releases/tag/v1.0.0) ViewModel是一个搭配ComponentLifecycle,在组件销毁时自动调用ViewModel的clean方法 ### 类 ViewModel 方法 clean 在组件销毁时自动调用 ### 装饰器 BindViewModel。 ## 下载安装 ```` ohpm install @duke/view-model ```` OpenHarmony ohpm 环境配置等更多内容,请参考[如何安装 OpenHarmony ohpm 包](https://gitee.com/openharmony-tpc/docs/blob/master/OpenHarmony_har_usage.md) ## 使用说明 ``` import { BindViewModel } from '@duke/view-model'; import { IndexViewModel } from '../IndexViewModel'; @Entry @Component struct Index { @State message: string = 'Hello World'; @BindViewModel viewModel:IndexViewModel = new IndexViewModel(); build() { RelativeContainer() { Text(this.message) .id('HelloWorld') .fontSize($r('app.float.page_text_font_size')) .fontWeight(FontWeight.Bold) .alignRules({ center: { anchor: '__container__', align: VerticalAlign.Center }, middle: { anchor: '__container__', align: HorizontalAlign.Center } }) .onClick(() => { this.message = 'Welcome'; }) } .height('100%') .width('100%') } } import { ViewModel } from "@duke/view-model"; export class IndexViewModel extends ViewModel{ } ``` ## 约束与限制 在下述版本验证通过: DevEco Studio: 5.0.5.315, SDK: HarmonyOS 5.0.1 Release Ohos_sdk_public 5.0.1.115 (API Version 13 Release) ## 目录结构 ```` |---- ViewModel | |---- AppScrope # 示例代码文件夹 | |---- entry # 示例代码文件夹 | |---- library # ViewModel库文件夹 | |---- index.ts # 对外接口 | |---- README.md # 安装使用方法 | |---- CHANGELOG.md # 更新日志 ```` ## 贡献代码 使用过程中发现任何问题都可以提 [Issue](https://gitee.com/Duke_Bit/component-lifecycle/issues) 给我,当然,我也非常欢迎你给我发 [PR](https://gitee.com/Duke_Bit/component-lifecycle) 。 ## 开源协议 本项目基于 [MIT license](https://gitee.com/Duke_Bit/component-lifecycle/blob/master/LICENSE) ,请自由地享受和参与开源。 ## 其他库 - [@duke/logan-ext](https://ohpm.openharmony.cn/#/cn/detail/@duke%2Flogan-ext) Logan扩展库,方便开箱即用: [https://gitee.com/Duke_Bit/logan](https://gitee.com/Duke_Bit/logan) - [@duke/logan](https://ohpm.openharmony.cn/#/cn/detail/@duke%2Flogan) Logan是一个为OpenHarmony开发的日志库,对美团技术团队的Logan的鸿蒙化移植版本: [https://gitee.com/Duke_Bit/logan](https://gitee.com/Duke_Bit/logan) - [@duke/websocket-client](https://ohpm.openharmony.cn/#/cn/detail/@duke%2Fwebsocket-client) WebSocket库解决官方API的一些bug问题: [https://gitee.com/Duke_Bit/websocket](https://gitee.com/Duke_Bit/websocket) - [@duke/component-lifecycle](https://ohpm.openharmony.cn/#/cn/detail/@duke%2Fcomponent-lifecycle) 鸿蒙版本的Lifecycle库,支持组件、Router和Navigation: [https://gitee.com/Duke_Bit/component-lifecycle](https://gitee.com/Duke_Bit/component-lifecycle) - [eventpost](https://ohpm.openharmony.cn/#/cn/detail/eventpost) 事件分发,支持组件中的lifecycle,在组件中使用自动取消订阅: [https://gitee.com/Duke_Bit/eventpost](https://gitee.com/Duke_Bit/eventpost) - [@duke/elf-dialog](https://ohpm.openharmony.cn/#/cn/detail/@duke%2Felf-dialog) CustomDialog黑魔法 不依赖promptAction 实现的函数级弹窗,省去复杂的模版代码,让你专注于你的业务,一行代码搞定弹窗: [https://gitee.com/Duke_Bit/elf-dialog](https://gitee.com/Duke_Bit/elf-dialog) - [@duke/elf-refresh](https://ohpm.openharmony.cn/#/cn/detail/@duke%2Felf-refresh) OpenHarmony 刷新组件,支持下拉刷新和上拉加载更多,支持各种组件,List、Grid,支持header,footer,目标打造HarmonyOS的SmartRefreshLayout: [https://gitee.com/Duke_Bit/ElfRefresh](https://gitee.com/Duke_Bit/ElfRefresh)