react 项目教程 (路由跳转 Tabbar 制作)

下面我们来写一个tabbar 组件 (antd-mobiel 有这样的组件)

首先我们需要在src 新建一个目录 为components (放置我们的组件) 并创建index.js ->为了将我们的组件统一导出。

创建 Tabbar 文件夹, 并且在目录下创建index.js ->来构建我们的组件内容

目录结构图片

在components 的index中导出组件

export { default as Tabbar} from './Tabbar.js'

为了动态渲染tabbar ,需要在routes.js的每个路由下面配置 icon text 等数据方便渲染

准备工作 ->
从 iconfont 来找到自己想要的图标


这是我的项目要用到的图标

生成代码后我们吧代码复制到 tabbar.less 中。
并复制每个图标的代码 添加到routes中对应的页面中去。 我们遍历数组需要 设置isNav 来 判断是否要渲染为tabbar 则此时我的 routes如下

import {
  Home, Mall, Detail, Cart, Mine, Login, NoPages
} from './pages'

const routes = [{
  path: '/home',
  text: '首页',
  isNav: true,
  icon: '',
  component: Home
}, {
  path: '/mall',
  text : '商城',
  isNav: true,
  icon: '',
  component: Mall
}, {
  path: '/mine',
  text: '我的',
  isNav: true,
  icon: '',
  component: Mine
}, {
  path: '/cart',
  text: '购物车',
  isNav: true,
  icon: '',
  component: Cart
}, {
  path: '/login',
  component: Login
}, {
  path: '/mall/detail/:id',
  isNav: false,
  component: Detail
}, {
  path: '/404',
  isNav: false,
  component: NoPages
}
];
export default routes

在我们要渲染的界面导入Tabbar 并且在 底部引用
我们可以通过import routes from '你的routes路径'传递配置的路径参数,也可以通过 组件传参的方式 在App 中 的Tabbar 标签处传递参数
在我们渲染的时候 如果 isNav 为true 时才要生成tabbar
所以传递的参数应该是

import { Tabbar } from './components'
const tabbarRoutes = routes.filter(route => route.isNav === true)


         <footer>
           <Tabbar routes = {routes}/>
         </footer>

此时 我们在Tabbar 界面进行 打印的时候 ,浏览器会打印出


浏览器会打印出isNav为true的routes列表

实现点击效果有两种方式 一种是 Link 一种是 history

先写

第一种方式 NavLink

在Tabbar中

import React, { Component } from 'react'
import { NavLink as Link } from 'react-router-dom'
import './tabbar.less'
export default class Tabbar extends Component {
render() {
  console.log(this.props)
  return (
    <div className = "m-tabbar">
      {
        this.props.routes.map(route => {
          console.log(route)
          return(
            <Link 
            key={route.path} 
            to = {route.path} 
            className = 'tabbaritem'>

            <span 
            dangerouslySetInnerHTML = {{ __html: route.icon }}
            className = 'icon'
            ></span>

            <div className = "title" >{route.text}</div>
            </Link>
          )
        })
      }
    </div>
  )
}
}

这样就可以实现路由跳转。 to是你要跳转到的页面
为了样式的美观 可以为路由加上 样式 我们写在 tabbar.less 中
我们需要在文件中 引入 生成的icon代码 @font-face
并把显示icon 的span 的font-family 设置为 iconfont

@font-face {
//引入iconfont
  font-family: 'iconfont';  /* project id 943948 */
  src: url('//at.alicdn.com/t/font_943948_pcz8qvoxvxi.eot');
  src: url('//at.alicdn.com/t/font_943948_pcz8qvoxvxi.eot?#iefix') format('embedded-opentype'),
  url('//at.alicdn.com/t/font_943948_pcz8qvoxvxi.woff') format('woff'),
  url('//at.alicdn.com/t/font_943948_pcz8qvoxvxi.ttf') format('truetype'),
  url('//at.alicdn.com/t/font_943948_pcz8qvoxvxi.svg#iconfont') format('svg');
}
.m {
  &-tabbar{
    background-color: #f1f1f1;
    width: 100%;
    height: 12vw;
    display: flex;
    align-items: center;
    position: fixed;
    bottom: 0;
    left: 0;
    justify-content: space-around;
    .tabbaritem{
      .icon{
        font-family: 'iconfont';
//将span 的font-family 改成 icon
        color: #666666;
        font-size: 16px;
      }
      .title{
        margin-top:2vw;
        color: #666666;
      }
    }
    .active{
      .icon{
      color: #c81c28;
      }
      .title{
      color: #c81c28;      
      }
    }
  }
}
tabbar显示后如图

第二种方式

第二张方式不需要引入 react-router-dom

class Tabbar extends Component {
    // static getDerivedStateFramProps (props){  第一种方式通过外面传来的参数来进行操作
    //     return {
    //         currentTab: props.routes[0].path
    //     }
    // }
    // constructor(props){
    //     super(props);
    //     this.state = {
    //         currentTab : ''
    //     }
    // }
   onItemClick = (path) =>{
    this.props.history.push(path);
    // console.log(this.props);
   }
   render() {
      const currentPath = this.props.location.pathname;
      // console.log(currentPath);
      const detail = '/detail'
      // console.log(currentPath.indexOf(detail));
      if(currentPath.indexOf(detail)=== -1){
    return (

      <div className = "ani-tabbar ">
      {
          this.props.routes.map(route =>{
            const cls = (route.path === currentPath) ? 'ani-tabbar-item active' : 'ani-tabbar-item'
            //   const csl = route.path === this.state.currentTab
           return (
            <div 
            key = {route.path}
             className={cls}
             onClick = {this.onItemClick.bind(this,route.path)}
              >
              {/* {this.getIcon(route.path)} */}
            <span className='icon' dangerouslySetInnerHTML = {{__html : route.icon}}></span>
            <span className= "text">{route.title}</span>
            </div>
           ) 
        })
      } 
      </div>
    )}
    else{
      return (<div></div>)
    }
  }
    
}

完成工作后需要进行代码提交 用git git 可以在完成之前用
feat : xxxxxx
修复bug 用
fix: xxxxxx

下面我们要配置redux及 axios
>https://www.jianshu.com/p/2bda61916d11

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,015评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,262评论 1 292
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,727评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,986评论 0 205
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,363评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,610评论 1 219
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,871评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,582评论 0 198
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,297评论 1 242
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,551评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,053评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,385评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,035评论 3 236
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,079评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,841评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,648评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,550评论 2 270

推荐阅读更多精彩内容