fix(wish-tree): 修复切换树后导航消失和树类型错误
Build and Deploy Server / build (push) Failing after 2s

- loadWishes使用safeInitRibbons/safeInitDanmaku,防止初始化出错导致页面崩溃
- getCurrentTree确保返回带type字段的有效树对象
- 添加错误处理,避免未定义错误
This commit is contained in:
gouki
2026-08-07 22:49:13 +00:00
parent b5bdf4c88d
commit bb9c19c99f
5 changed files with 153 additions and 90 deletions
+32 -3
View File
@@ -172,8 +172,8 @@ Page({
if (res.code === 0) {
this.setData({ wishes: res.data.list || [] });
this.initRibbons();
this.initDanmaku();
this.safeInitRibbons();
this.safeInitDanmaku();
}
} catch (err) {
console.error('加载许愿失败:', err);
@@ -181,8 +181,28 @@ Page({
this.setData({
wishes: this.getMockWishes(),
});
this.safeInitRibbons();
this.safeInitDanmaku();
}
},
// 安全初始化丝带
safeInitRibbons() {
try {
this.initRibbons();
} catch (err) {
console.error('初始化丝带失败:', err);
this.ribbons = [];
}
},
// 安全初始化弹幕
safeInitDanmaku() {
try {
this.initDanmaku();
} catch (err) {
console.error('初始化弹幕失败:', err);
this.danmaku = [];
}
},
@@ -931,7 +951,16 @@ Page({
// 获取当前树
getCurrentTree() {
const { trees, currentTreeIndex } = this.data;
return trees[currentTreeIndex] || { type: 'pine' };
const tree = trees[currentTreeIndex];
// 确保返回有效的树对象,带默认类型
if (!tree) {
return { type: 'pine', name: '许愿树' };
}
// 确保有type字段
if (!tree.type) {
tree.type = 'pine';
}
return tree;
},
// 切换树