Files
toutoukan_front/miniprogram/pages/home/home.ts
JACKYMYPERSON 940fb884c6 2025-9-24-21:10
2025-09-24 21:10:29 +08:00

55 lines
1.5 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
Component({
data: {
// 用对象存储多个卡片的选中状态键为卡片ID值为选中的选项ID
selectedOptions: {} as Record<string, string>
},
methods: {
/**
* 选择选项
* @param e 事件对象包含卡片ID和选项ID
*/
selectOption(e: WechatMiniprogram.TouchEvent) {
const { card, id } = e.currentTarget.dataset as { card: string; id: string };
// 更新选中状态(使用对象展开语法保持响应式)
this.setData({
selectedOptions: {
...this.data.selectedOptions,
[card]: id
}
});
},
/**
* 提交投票
* @param e 事件对象包含卡片ID
*/
submitVote(e: WechatMiniprogram.TouchEvent) {
const { card } = e.currentTarget.dataset as { card: string };
const selected = this.data.selectedOptions[card];
if (!selected) {
wx.showToast({
title: '请先选择一个选项',
icon: 'none',
duration: 2000
});
return;
}
// 可以通过triggerEvent向父组件传递投票结果
this.triggerEvent('voteSuccess', {
cardId: card,
selectedOption: selected,
timestamp: Date.now()
});
wx.showToast({
title: '投票成功',
icon: 'success',
duration: 2000
});
}
}
});