新增mvvm示例

This commit is contained in:
YHH
2025-07-08 20:23:19 +08:00
parent 731edf5872
commit 2925ee380d
20 changed files with 4164 additions and 1673 deletions

View File

@@ -0,0 +1,41 @@
import { ViewModel, observable, computed, command } from '@esengine/mvvm-ui-framework';
/**
* 商店视图模型
*/
export class ShopViewModel extends ViewModel {
public get name(): string {
return 'ShopViewModel';
}
@observable
selectedCategory: string = 'weapons';
@observable
playerGold: number = 1000;
@observable
cartItems: any[] = [];
@computed(['cartItems'])
get totalPrice(): number {
return this.cartItems.reduce((total, item) => total + item.price, 0);
}
@computed(['playerGold', 'totalPrice'])
get canPurchase(): boolean {
return this.playerGold >= this.totalPrice;
}
@command()
public addToCart(item: any): void {
this.cartItems.push(item);
}
@command('canPurchase')
public purchase(): void {
this.playerGold -= this.totalPrice;
this.cartItems = [];
}
}

View File

@@ -0,0 +1,9 @@
{
"ver": "4.0.24",
"importer": "typescript",
"imported": true,
"uuid": "148fe394-03cd-45a1-9bc0-5cb24440d8db",
"files": [],
"subMetas": {},
"userData": {}
}