tabbar.js 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import { defineStore } from 'pinia';
  2. import { ref } from 'vue';
  3. export const useTabbarStore = defineStore('tabbar', () => {
  4. // 当前选中的 tab 索引
  5. const currentIndex = ref(0);
  6. // 首页是否显示回到顶部
  7. const showBackTop = ref(false);
  8. // 滚动阈值(超过这个值显示回到顶部)
  9. const scrollThreshold = 300;
  10. // 设置当前 tab 索引
  11. const setCurrentIndex = (index) => {
  12. currentIndex.value = index;
  13. };
  14. // 设置是否显示回到顶部
  15. const setShowBackTop = (show) => {
  16. showBackTop.value = show;
  17. };
  18. // 根据页面路径获取 tab 索引
  19. const getIndexByPath = (path) => {
  20. const tabPaths = [
  21. '/pages/index/index',
  22. '/pages/goods_cate/goods_cate',
  23. '/pages/order_addcart/order_addcart',
  24. '/pages/user/index'
  25. ];
  26. return tabPaths.findIndex(p => path.includes(p.replace('/pages/', '')));
  27. };
  28. return {
  29. currentIndex,
  30. showBackTop,
  31. scrollThreshold,
  32. setCurrentIndex,
  33. setShowBackTop,
  34. getIndexByPath
  35. };
  36. });