- loadWishes使用safeInitRibbons/safeInitDanmaku,防止初始化出错导致页面崩溃 - getCurrentTree确保返回带type字段的有效树对象 - 添加错误处理,避免未定义错误
This commit is contained in:
@@ -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;
|
||||
},
|
||||
|
||||
// 切换树
|
||||
|
||||
Reference in New Issue
Block a user