react手机端TabBar用法

在路由组件中使用嵌套路由

import React from 'react'
import { TabBar } from 'antd-mobile';
import {Route} from 'react-router-dom'
import Index from '../../pages/Index' // 引入子组件,下面生成嵌套路由
import HouseList from '../../pages/HouseList' // 引入子组件,下面生成嵌套路由
import News from '../../pages/News' // 引入子组件,下面生成嵌套路由
import Profile from '../../pages/Profile' // 引入子组件,下面生成嵌套路由
// 导入自己组件内部的样式文件
import './index.css'
export default class Home extends React.Component {
  state = {
    selectedTab: this.props.location.pathname, // 默认选中哪个tabbar
    // hidden: false, // 用于控制tabar显示与隐藏的... 这里不需要隐藏,去掉
    // fullScreen: false, // 是否是全屏,tabbar在底部...此项目不需要动态控制TarbBar的位置,所以不要
  };
  render () {
    return (
      // 嵌套路由中不能用<Router></Router>包裹,否则路由将跳转不了
      // <Router>
      <div className="home">
        {/* 设置导航菜单 因为此处tabbar没有link功能,所以利用事件 + this.props.history.push('/home路径')进行路由跳转*/}
        {/* <Link to="/home/news">资讯</Link> */}
        {/* 配置路由 */}
        <Route path="/home/index" component={Index} />
        <Route path="/home/list" component={HouseList} />
        <Route path="/home/news" component={News} />
        <Route path="/home/profile" component={Profile} />
        {/* TabBar */}
        {/* <div style={this.state.fullScreen ? { position: 'fixed', height: '100%', width: '100%', top: 0 } : { height: 400 }}> */}
        <TabBar
          tintColor="#21b97a"
          barTintColor="white"
          // hidden={this.state.hidden}
          noRenderContent={true}
        >
          <TabBar.Item
            title="首页"
            key="Life"
            icon={
              <i className="iconfont icon-ind" />
            }
            selectedIcon={
              <i className="iconfont icon-ind" />
            }
            selected={this.state.selectedTab === '/home/index'}
            onPress={() => {
              this.setState({
                selectedTab: '/home/index',
              });
              // 路由切换
              this.props.history.push('/home/index')
            }}
            data-seed="logId"
          >
          </TabBar.Item>
          <TabBar.Item
            icon={
              <i className="iconfont icon-findHouse" />
            }
            selectedIcon={
              <i className="iconfont icon-findHouse" />
            }
            title="找房"
            key="Koubei"
            selected={this.state.selectedTab === '/home/list'}
            onPress={() => {
              this.setState({
                selectedTab: '/home/list',
              });
              // 路由切换
              this.props.history.push('/home/list')
            }}
            data-seed="logId1"
          >
          </TabBar.Item>
          <TabBar.Item
            icon={
              <i className="iconfont icon-infom" />
            }
            selectedIcon={
              <i className="iconfont icon-infom" />
            }
            title="资讯"
            key="Friend"
            selected={this.state.selectedTab === '/home/news'}
            onPress={() => {
              this.setState({
                selectedTab: '/home/news',
              });
              // 路由切换
              this.props.history.push('/home/news')
            }}
          >
          </TabBar.Item>
          <TabBar.Item
            icon={
              <i className="iconfont icon-my" />
            }
            selectedIcon={
              <i className="iconfont icon-my" />
            }
            title="我的"
            key="my"
            selected={this.state.selectedTab === '/home/profile'}
            onPress={() => {
              this.setState({
                selectedTab: '/home/profile',
              });
              // 路由切换
              this.props.history.push('/home/profile')
            }}
          >
          </TabBar.Item>
        </TabBar>
      </div>
      // </Router>
    )
  }
}

在这里插入图片描述
里面设置了通过切换图标改变浏览器url地址,也通过切换浏览器url地址高亮当前对应的TabBar子页

2.优化:代码整合

import React from 'react'
import { TabBar } from 'antd-mobile';
import {Route} from 'react-router-dom'
import Index from '../../pages/Index' // 引入子组件,下面生成嵌套路由
import HouseList from '../../pages/HouseList' // 引入子组件,下面生成嵌套路由
import News from '../../pages/News' // 引入子组件,下面生成嵌套路由
import Profile from '../../pages/Profile' // 引入子组件,下面生成嵌套路由
// 导入自己组件内部的样式文件
import './index.css'
// TabBar 数据
const tabItems = [
  {
    title: '首页',
    icon: 'icon-ind',
    path: '/home/index'
  },
  {
    title: '找房',
    icon: 'icon-findHouse',
    path: '/home/list'
  },
  {
    title: '资讯',
    icon: 'icon-infom',
    path: '/home/news'
  },
  {
    title: '首页',
    icon: 'icon-my',
    path: '/home/profile'
  }
]
export default class Home extends React.Component {
  state = {
    selectedTab: this.props.location.pathname, // 默认选中哪个tabbar
    // hidden: false, // 用于控制tabar显示与隐藏的... 这里不需要隐藏,去掉
    // fullScreen: false, // 是否是全屏,tabbar在底部...此项目不需要动态控制TarbBar的位置,所以不要
  };
  // 渲染TabBar.Item
  renderTabBarItem() {
    return tabItems.map(item => {
      return (
        <TabBar.Item
          title={item.title}
          key={item.title}
          icon={
            <i className={`iconfont ${item.icon}`} />
          }
          selectedIcon={
            <i className={`iconfont ${item.icon}`} />
          }
          selected={this.state.selectedTab === item.path}
          onPress={() => {
            this.setState({
              selectedTab: item.path,
            });
            // 路由切换
            this.props.history.push(item.path)
          }}
          data-seed="logId"
        ></TabBar.Item>
      )
    })
  }
  render () {
    return (
      // 嵌套路由中不能用<Router></Router>包裹,否则路由将跳转不了
      // <Router>
      <div className="home">
        {/* 设置导航菜单 因为此处tabbar没有link功能,所以利用事件 + this.props.history.push('/home路径')进行路由跳转*/}
        {/* <Link to="/home/news">资讯</Link> */}
        {/* 配置路由 */}
        <Route path="/home/index" component={Index} />
        <Route path="/home/list" component={HouseList} />
        <Route path="/home/news" component={News} />
        <Route path="/home/profile" component={Profile} />
        {/* TabBar */}
        {/* <div style={this.state.fullScreen ? { position: 'fixed', height: '100%', width: '100%', top: 0 } : { height: 400 }}> */}
        <TabBar
          tintColor="#21b97a"
          barTintColor="white"
          // hidden={this.state.hidden}
          noRenderContent={true}
        >
          {this.renderTabBarItem()}
        </TabBar>
      </div>
      // </Router>
    )
  }
}

3.首页路由的设置

在这里插入图片描述

// TabBar 数据
const tabItems = [
  {
    title: '首页',
    icon: 'icon-ind',
    // path: '/home/index' // 去掉/index
    path: '/home'
  },
  {
    title: '找房',
    icon: 'icon-findHouse',
    path: '/home/list'
  },
  {
    title: '资讯',
    icon: 'icon-infom',
    path: '/home/news'
  },
  {
    title: '首页',
    icon: 'icon-my',
    path: '/home/profile'
  }
]

去掉首页中的/index

3-2在路由中添加一条默认路由,当浏览器url的pathname为/时也跳转到首页
其中<Route exact path="/" render={ () => } /> render属性:是一个函数prop,用于指定要渲染的内容
Redirect组件用于实现【路由重定向】,to属性指定要跳转到的路由地址

<Route exact path="/" render={ () => <Redirect to="/home"/> } />
<Route path="/home" component={Home}></Route>
<Route path="/citylist" component={CityList}></Route>

完整代码,可忽略
<Route exact path="/" render={ () => } />是加到父组件路由同级的
Redirect 要从路由里面引用 import { BrowserRouter as Router, Route, Redirect } from ‘react-router-dom’

App.js文件

import React from 'react';
// 导入要使用的组件
// import { Button } from 'antd-mobile'
// 引入路由
import { BrowserRouter as Router, Route, Redirect } from 'react-router-dom'
// 设置两个组件的路由,要先导入组件(页面)
import Home from './pages/Home' // /index可省略
import CityList from './pages/CityList'
function App() {
  return (
    <Router>
      <div className="App">
        {/* <Button>登录</Button> */}
        {/* 配置导航菜单 ,项目中暂时不要,因为直接展示首页,而不是需要点击才跳转到首页*/}
        {/* <Link to="/home">首页</Link>
        <Link to="/citylist">城市选择</Link> */}
        {/* 配置路由 */}
        <Route exact path="/" render={ () => <Redirect to="/home"/> } />
        <Route path="/home" component={Home}></Route>
        <Route path="/citylist" component={CityList}></Route>
      </div>
    </Router>
  );
}

export default App;

Home组件:嵌套路由

import React from 'react'
import { TabBar } from 'antd-mobile';
import {Route} from 'react-router-dom'
import Index from '../../pages/Index' // 引入子组件,下面生成嵌套路由
import HouseList from '../../pages/HouseList' // 引入子组件,下面生成嵌套路由
import News from '../../pages/News' // 引入子组件,下面生成嵌套路由
import Profile from '../../pages/Profile' // 引入子组件,下面生成嵌套路由
// 导入自己组件内部的样式文件
import './index.css'
import { Redirect } from 'react-router-dom/cjs/react-router-dom.min';
// TabBar 数据
const tabItems = [
  {
    title: '首页',
    icon: 'icon-ind',
    path: '/home'
  },
  {
    title: '找房',
    icon: 'icon-findHouse',
    path: '/home/list'
  },
  {
    title: '资讯',
    icon: 'icon-infom',
    path: '/home/news'
  },
  {
    title: '首页',
    icon: 'icon-my',
    path: '/home/profile'
  }
]
export default class Home extends React.Component {
  state = {
    selectedTab: this.props.location.pathname, // 默认选中哪个tabbar
    // hidden: false, // 用于控制tabar显示与隐藏的... 这里不需要隐藏,去掉
    // fullScreen: false, // 是否是全屏,tabbar在底部...此项目不需要动态控制TarbBar的位置,所以不要
  };
  // 渲染TabBar.Item
  renderTabBarItem() {
    return tabItems.map(item => {
      return (
        <TabBar.Item
          title={item.title}
          key={item.title}
          icon={
            <i className={`iconfont ${item.icon}`} />
          }
          selectedIcon={
            <i className={`iconfont ${item.icon}`} />
          }
          selected={this.state.selectedTab === item.path}
          onPress={() => {
            this.setState({
              selectedTab: item.path,
            });
            // 路由切换
            this.props.history.push(item.path)
          }}
          data-seed="logId"
        ></TabBar.Item>
      )
    })
  }
  render () {
    return (
      // 嵌套路由中不能用<Router></Router>包裹,否则路由将跳转不了
      // <Router>
      <div className="home">
        {/* 设置导航菜单 因为此处tabbar没有link功能,所以利用事件 + this.props.history.push('/home路径')进行路由跳转*/}
        {/* <Link to="/home/news">资讯</Link> */}
        {/* 配置路由 */}
        <Route exact path="/home" component={Index} />
        <Route path="/home/list" component={HouseList} />
        <Route path="/home/news" component={News} />
        <Route path="/home/profile" component={Profile} />
        {/* TabBar */}
        {/* <div style={this.state.fullScreen ? { position: 'fixed', height: '100%', width: '100%', top: 0 } : { height: 400 }}> */}
        <TabBar
          tintColor="#21b97a"
          barTintColor="white"
          // hidden={this.state.hidden}
          noRenderContent={true}
        >
          {this.renderTabBarItem()}
        </TabBar>
      </div>
      // </Router>
    )
  }
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值