底部tab组件的封装

//Home.js

import React,{useState,useEffect} from 'react'
import {  TabBar } from 'antd-mobile'
import { Tabs, Badge } from 'antd-mobile'
import {useSelector} from 'react-redux'
import {UserOutline,AppstoreOutline} from 'antd-mobile-icons'
import { ShoppingCartO,WapHomeO  } from '@react-vant/icons'
import './Home.scss'
import { Search } from 'react-vant';
import {Outlet,useNavigate,useLocation} from 'react-router-dom'
export default function Home() {
  const [value, setValue] = useState('');
  const navigate=useNavigate()
  const state=useSelector(state=>state)
  console.log(state);
  const location=useLocation()
  useEffect(()=>{
    //保证刷新页面 url和tab匹配
    setActive(location.pathname)
  },[])
  function changeTab(key){
    console.log(key);
    //修改active
    setActive(key)
    //跳转路由界面  路由动态
    navigate(key)
  }

  const [active, setActive] = useState('/')
  return (
    <div className='container1'>
      <Outlet></Outlet>

      <div className="footer">
        <TabBar activeKey={active} onChange={changeTab}>
            
            <TabBar.Item key={'/'} icon={ <WapHomeO  />} title={'首页'} />
            <TabBar.Item key={'/cate'} icon={ AppstoreOutline } title={'分类'} />
            <TabBar.Item key={'/car'} icon={ <ShoppingCartO  />} badge={state.car.reduce((init,item)=>init+item.sum,0)} title={'购物车'} />
            <TabBar.Item key={'/my'} icon={ UserOutline}  title={'个人'} />
        </TabBar>

      </div>
       
    </div>
  )
}


.container1{
    width: 100%;
    height: 100%;

    
    .footer{
        position: fixed;
        bottom: 0;
        left:0;
        right: 0;
        background-color: #fff;
    }
}



//router.js


import Car from "./components/Car"
import Cate from "./components/Cate"
import First from "./components/First"
import Home from "./components/Home"
import My from "./components/My"
import Search from "./components/Search"


const routes=[
    
    {
        path:'/',
        element:<Home></Home>, 
        children:[
            {path:'/',element:<First></First>},
            {
                path:'/cate',element:<Cate></Cate>
            },
            {
                path:'/car',element:<Car></Car>
            },
            {
                path:'/my',element:<My></My>
            }

        ] 
    },
    {
        path:'/search',
        element:<Search></Search>
    }
   
        
]

export default routes

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值