feat(entry): 建立 MVVM+data 分层骨架,接入 V2 状态管理与 Navigation 路由

This commit is contained in:
AndyChen1024
2026-09-11 16:22:45 +08:00
parent 3a116df871
commit 058b978f25
11 changed files with 336 additions and 33 deletions

View File

@@ -0,0 +1,41 @@
import { User } from '../model/User';
import { UserRepository } from '../data/repository/UserRepository';
import { ServiceLocator } from '../di/ServiceLocator';
/**
* 首页视图模型:@ObservedV2 三态(loading / error / data)。
* 只依赖 UserRepository 契约,不直接接触网络层。
*/
@ObservedV2
export class HomeViewModel {
@Trace users: User[] = [];
@Trace loading: boolean = false;
@Trace error: string = '';
private readonly repository: UserRepository;
constructor(repository: UserRepository) {
this.repository = repository;
}
/**
* 工厂:从 DI 容器解析依赖并装配,避免页面直接触碰 data 层类型。
*/
static create(): HomeViewModel {
return new HomeViewModel(ServiceLocator.get('userRepository') as UserRepository);
}
async load(): Promise<void> {
this.loading = true;
this.error = '';
try {
// 演示:拉取一个示例用户;真实业务可替换为列表接口。
const user = await this.repository.getUser('demo-user');
this.users = [user];
} catch (err) {
this.error = JSON.stringify(err);
} finally {
this.loading = false;
}
}
}