100 lines
2.1 KiB
JavaScript
100 lines
2.1 KiB
JavaScript
Component({
|
|
data: {
|
|
selected: 0,
|
|
color: "#7A7E83",
|
|
selectedColor: "#4A90E2",
|
|
list: [
|
|
{
|
|
emoji: "🏠",
|
|
text: "首页",
|
|
pagePath: "/pages/index/index"
|
|
},
|
|
{
|
|
emoji: "📚",
|
|
text: "课程",
|
|
pagePath: "/pages/courses/courses"
|
|
},
|
|
{
|
|
emoji: "💬",
|
|
text: "论坛",
|
|
pagePath: "/pages/forum/forum"
|
|
},
|
|
{
|
|
emoji: "🔧",
|
|
text: "工具",
|
|
pagePath: "/pages/tools/tools"
|
|
},
|
|
{
|
|
emoji: "👤",
|
|
text: "我的",
|
|
pagePath: "/pages/my/my"
|
|
}
|
|
]
|
|
},
|
|
|
|
attached() {
|
|
try {
|
|
// 获取当前页面路径
|
|
const pages = getCurrentPages();
|
|
const currentPage = pages[pages.length - 1];
|
|
const url = currentPage ? `/${currentPage.route}` : '';
|
|
|
|
// 匹配当前选中的tab
|
|
this.data.list.forEach((item, index) => {
|
|
if (item.pagePath === url) {
|
|
this.setData({
|
|
selected: index
|
|
});
|
|
}
|
|
});
|
|
} catch (e) {
|
|
console.error('[TabBar] attached错误:', e);
|
|
}
|
|
},
|
|
|
|
methods: {
|
|
switchTab(e) {
|
|
try {
|
|
const data = e.currentTarget.dataset;
|
|
const url = data.path;
|
|
const index = data.index;
|
|
|
|
if (!url) {
|
|
console.error('[TabBar] 页面路径为空');
|
|
wx.showToast({
|
|
title: '页面路径错误',
|
|
icon: 'none'
|
|
});
|
|
return;
|
|
}
|
|
|
|
// 先更新选中状态
|
|
this.setData({
|
|
selected: index
|
|
});
|
|
|
|
// 切换tab
|
|
wx.switchTab({
|
|
url,
|
|
success: () => {
|
|
console.log('[TabBar] 切换成功:', url);
|
|
},
|
|
fail: (err) => {
|
|
console.error('[TabBar] 切换失败:', err);
|
|
wx.showToast({
|
|
title: '页面切换失败',
|
|
icon: 'none'
|
|
});
|
|
}
|
|
});
|
|
} catch (e) {
|
|
console.error('[TabBar] switchTab错误:', e);
|
|
wx.showToast({
|
|
title: '程序异常,请重试',
|
|
icon: 'none'
|
|
});
|
|
}
|
|
}
|
|
}
|
|
});
|