赞
踩
/** | |
* 网络请求MVVM数据模型,由子类实现状态机管理,由方法实现回调监听 | |
*/ | |
export abstract class LoadState { | |
/** | |
* loading函数,如果当前状态是Loading,则调用回调函数 | |
* @param callBack 回调函数 | |
* @returns this | |
*/ | |
loading(callBack?: () => void): this { | |
if (this instanceof Loading) { | |
callBack?.call(null); | |
} | |
return this; | |
} | |
/** | |
* loaded函数,如果当前状态是Loaded,则调用回调函数 | |
* @param callBack 回调函数 | |
* @returns this | |
*/ | |
loaded(callBack?: (result: Loaded<any>) => void): this { | |
if (this instanceof Loaded) { | |
callBack?.call(null, this); | |
} | |
return this; | |
} | |
/** | |
* loadError函数,如果当前状态是LoadError,则调用回调函数 | |
* @param callBack 回调函数 | |
* @returns this | |
*/ | |
loadError(callBack?: (error: LoadError) => void): this { | |
if (this instanceof LoadError) { | |
callBack?.call(null, this); | |
} | |
return this; | |
} | |
} | |
/** | |
* Loading类,继承自LoadState类 | |
*/ | |
export class Loading extends LoadState {} | |
/** | |
* Loaded类,继承自LoadState类,包含一个result属性和一个data方法 | |
*/ | |
export class Loaded<T> extends LoadState { | |
result?: T; | |
constructor(data: T) { | |
super(); | |
this.result = data; | |
} | |
data(): T | undefined { | |
return this.result; | |
} | |
} | |
/** | |
* LoadError类,继承自LoadState类,包含code和message属性 | |
*/ | |
export class LoadError extends LoadState { | |
code?: number; | |
message?: string; | |
constructor(code: number, message: string) { | |
super(); | |
this.code = code; | |
this.message = message; | |
} | |
} | |
/** | |
* ValueNotifier类,包含_value、listeners属性和addListener、notifyListeners、value方法 | |
*/ | |
export class ValueNotifier<T> { | |
private _value: T; | |
listeners: Array<() => void> = []; | |
constructor(value: T) { | |
this._value = value; | |
} | |
get value(): T { | |
return this._value; | |
} | |
set value(value: T) { | |
this._value = value; | |
this.notifyListeners(); | |
} | |
addListener(listener: () => void) { | |
this.listeners.push(listener); | |
} | |
notifyListeners() { | |
for (let listener of this.listeners) { | |
listener(); | |
} | |
} | |
} |
以获取一个车辆详情的场景来模拟网络请求和数据处理
import { Loaded, LoadError, Loading, LoadState, ValueNotifier } from './LoadState'; | |
export class VehicleViewModel { | |
lsVehicleDetail: ValueNotifier<LoadState | null>; | |
constructor() { | |
this.lsVehicleDetail = new ValueNotifier<LoadState | null>(null); | |
} | |
// 获取车辆详情 | |
async getVehicleDetail() { | |
// 发起请求 | |
this.lsVehicleDetail.value = new Loading(); | |
await new Promise(resolve => setTimeout(resolve, 3000)); | |
// 获得数据 | |
this.lsVehicleDetail.value = new Loaded("aa"); | |
await new Promise(resolve => setTimeout(resolve, 3000)); | |
// 模拟网络报错 | |
this.lsVehicleDetail.value = new LoadError(123, "error"); | |
} | |
} |
@Component | |
export struct VehicleComponent { | |
private vm: VehicleViewModel = new VehicleViewModel(); | |
aboutToAppear() { | |
this.vm.lsVehicleDetail.addListener(() => { | |
this.vm.lsVehicleDetail.value?.loading(() => { | |
// 开始网络请求 | |
console.log(`hello1:start Loading`); | |
}).loaded((result) => { | |
let data = result?.data() as String | |
console.log(`hello2:${result} - ${data}`); | |
}).loadError((error) => { | |
console.log(`hello3:${error?.code} - ${error?.message}`); | |
}); | |
}); | |
} | |
} |
hello1:start Loading | |
hello2:[object Object] - aa | |
hello3:123 - error |
最后,为了能让大家更好的去学习提升鸿蒙 (Harmony OS) 开发技术,小编连夜整理了一份30个G纯血版学习资料(含视频、电子书、学习文档等)以及一份在Github上持续爆火霸榜的《纯血版华为鸿蒙 (Harmony OS)开发手册》(共计890页),希望对大家有所帮助。
需要以上视频学习资料小伙伴
这份手册涵盖了当前鸿蒙 (Harmony OS) 开发技术必掌握的核心知识点
HarmonyOS 概念:
如何快速入门?
开发基础知识:
基于ArkTS 开发:
获取以上文中提到的这份纯血版鸿蒙 (Harmony OS) 开发资料的小伙伴
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。