使用dva+umi+antd构建页面(三)

使用dva+umi+antd构建页面(三)

布局做好以后就可以开始构建整个系统路由及其对应的路由组件,但是有时候我们的需求是要求路由的页面在Tabs标签页中的,然后点击各个标签页可以进行切换,接下来就来探讨一下这种布局的实现。

Tabs组件

  • 添加Tabs组件,在src/components/layout中添加Tabs.js组件,首先在官网https://ant.design/components/tabs-cn/复制一个Tabs组件如下:

    import React from 'react'
    import {
          Tabs, Button } from 'antd';
    
    const TabPane = Tabs.TabPane;
    
    class Mytab extends React.Component {
         
      constructor(props) {
         
        super(props);
        this.newTabIndex = 0;
        const panes = [
          {
          title: 'Tab 1', content: 'Content of Tab Pane 1', key: '1' },
          {
          title: 'Tab 2', content: 'Content of Tab Pane 2', key: '2' },
        ];
        this.state = {
         
          activeKey: panes[0].key,
          panes,
        };
      }
    
      onChange = (activeKey) => {
         
        this.setState({
          activeKey });
      }
    
      onEdit = (targetKey, action) => {
         
        this[action](targetKey);
      }
    
      add = () => {
         
        const panes = this.state.panes;
        const activeKey = `newTab${
           this.newTabIndex++}`;
        panes.push({
          title: 'New Tab', content: 'New Tab Pane', key: activeKey });
        this.setState({
          panes, activeKey });
      }
    
      remove = (targetKey) => {
         
        let activeKey = this.state.activeKey;
        let lastIndex;
        this.state.panes.forEach((pane, i) => {
         
          if (pane.key === targetKey) {
         
            lastIndex = i - 1;
          }
        });
        const panes = this.state.panes.filter(pane => pane.key !== targetKey);
        if (panes.length && activeKey === targetKey) {
         
          if (lastIndex >= 0) {
         
            activeKey = panes[lastIndex].key;
          } else {
         
            activeKey = panes[0].key;
          }
        }
        this.setState({
          panes, activeKey });
      }
    
      render() {
         
        return (
          <div>
            <div style={
         {
          marginBottom: 16 }}>
              <Button onClick={
         this
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值