最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

react配合antd組件實(shí)現(xiàn)的管理系統(tǒng)示例代碼

 更新時(shí)間:2018年04月24日 09:32:00   作者:doudouyoutangtang  
這篇文章主要介紹了react配合antd組件實(shí)現(xiàn)的管理系統(tǒng)示例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧

前言:此文需要有一定react,redux基礎(chǔ),具體學(xué)習(xí)資料請(qǐng)科學(xué)上網(wǎng)。

使用create-react-app腳手架

具體基礎(chǔ)配置請(qǐng)參考

配合antd組件實(shí)現(xiàn)的管理系統(tǒng)demo,線上地址

開發(fā)前反思

1. 按需加載

webpack的 import 動(dòng)態(tài)加載的模塊的函數(shù),import(參數(shù)),參數(shù)為模塊地址。

注意: import 后會(huì)返回一個(gè)promise對(duì)象。

import('/components/chart').then(mud => {
  dosomething(mod)
});

本demo構(gòu)建了異步加載組件Bundle,具體代碼請(qǐng)見

class Bundle extends Component {
 constructor(props) {
   super(props);
   this.state = {
     mod: null
   };
 }
 unmount = false
 componentDidMount = () => {
  // 加載組件時(shí),打開全局loading
  this.props.dispatch(loading(true))
  this.load(this.props)
 }
 componentWillUnmount = () => {
  this.unmount = true
 }
 
 componentWillReceiveProps(nextProps) {
   if (nextProps.load !== this.props.load) {
     this.load(nextProps)
   }
 }
 load(props) {
   if (this.state.mod) {
     return true
   }
   //注意這里,使用Promise對(duì)象; mod.default導(dǎo)出默認(rèn)
   props.load().then((mod) => {
     if (this.unmount) {
       // 離開組件時(shí),不異步執(zhí)行setState
       this.props.dispatch(loading(false))
       return false
     }
     this.setState({
       mod: mod.default ? mod.default : mod
     }, _ => {
      // 組件加載完畢,關(guān)閉loading
      this.props.dispatch(loading(false))
     });
   });
 }

 render() {
   return this.state.mod ? this.props.children(this.state.mod) : null;
 }
}

具體使用

<Bundle load={() => import('路徑')}>
  {Comp => {
   return Comp ? <Comp /> : <div>加載中...</div>
  }}
 </Bundle>

2. 全局loading

配合redux,dispatch => reducer更新 => mapstate更新,在根組件進(jìn)行l(wèi)oading的渲染

詳細(xì)請(qǐng)見本demo地址 src/routers/router.js——render函數(shù)

3. 配置路由對(duì)象

項(xiàng)目布局如下

本demo使用的是router4,官方文檔演示為單行Route(如vue種的router),未有統(tǒng)一配置對(duì)象。 管理系統(tǒng)基本圍繞著content進(jìn)行業(yè)務(wù)開發(fā),構(gòu)建通用配置有助于開發(fā) 構(gòu)建router.config.js

const routers = [
 {
  menuName: '主頁(yè)',
  menuIco: 'home',
  component: 'home/home.js', // 主頁(yè)
  path: '/admin/home' // 主頁(yè)
 },
 {
  menuName: '用戶',
  menuIco: 'user',
  children: [
   {
    menuName: '用戶列表',
    component: 'user/list.js', // 主頁(yè)
    path: '/admin/user/list' // 主頁(yè)
   }
  ]
 },
 {
  menuName: '多級(jí)菜單',
  menuIco: 'setting',
  children: [
   {
    menuName: '多級(jí)菜單2',
    children: [
     {
      menuName: '菜單',
      component: 'user/list.js', // 主頁(yè)
      path: '/admin/user/list3' // 主頁(yè)
     }
    ]
   }
  ]
 },
 {
  menuName: '關(guān)于我',
  menuIco: 'smile-o',
  component: 'about/about.js', // 主頁(yè)
  path: '/admin/about' // 主頁(yè)
 }
]

實(shí)現(xiàn)思路,最外層布局為Admin,content被Admin包裹,那么可以利用 this.props.children ,把內(nèi)容打入content中。(利用bundle組件異步加載后塞入組件進(jìn)行渲染)

<Admin>
  <Content { ...this.props } breadcrumb={this.state.breadcrumb}>
    {this.props.children}
  </Content>
</Admin>
// Content組件內(nèi)部
render() {
  return (
    <div> 
      {this.props.children}
    </div>
  )
}
// 本demo實(shí)現(xiàn),詳見src/routers/router.js
<Route
 path="/admin"
 render={item => (
  <Admin {...item} { ...this.props }>
   {initRouters.map(el => this.deepItem(el, { ...this.props, ...item}))}
  </Admin>
 )}
/>

4. 配置通用reducer

多人配合開發(fā),一些業(yè)務(wù)場(chǎng)景的組件需要狀提升(不理解狀態(tài)提升的同學(xué),請(qǐng)科學(xué)上網(wǎng))

import otherReducers from './otherReducers'
const App = combineReducers({
  rootReducers,
  ...otherReducers // 其他需要增加的reducers
})

5. 登陸驗(yàn)證

利用 withRouter 函數(shù),頁(yè)面進(jìn)行路由跳轉(zhuǎn)時(shí)觸發(fā)該函數(shù)

const newWithRouter = withRouter(props => {
  // ....
})

若未登錄,則返回

return <Redirect to="/login" />

6. 路由攔截

同上,根據(jù)路由配置與權(quán)限,返回相應(yīng)的菜單或屏蔽

return <Redirect to={其他} />

7 其他配置

7-1. 自定義樣式

// 修改webpack.config.dev.js 和 webpack.config-prod.js 配置文件
{
  test: /\.(css|less)$/,
  // 匹配src的都自動(dòng)加載css-module
  include: [/src/],
  exclude: [/theme/],
  use: [
    require.resolve('style-loader'), {
      loader: require.resolve('css-loader'),
      options: {
        importLoaders: 1,
        modules: true, // 新增對(duì)css modules的支持
        localIdentName: '[path]___[name]__[local]___[hash:base64:5]'
      }
    }, {
      loader: require.resolve('postcss-loader'),
      options: {
        // Necessary for external CSS imports to work
        // https://github.com/facebookincubator/create-react-app/issues/2677
        ident: 'postcss',
        plugins: () => [
          require('postcss-flexbugs-fixes'),
          autoprefixer({
            browsers: [
              '>1%', 'last 4 versions', 'Firefox ESR', 'not ie < 9', // React doesn't support IE8 anyway
            ],
            flexbox: 'no-2009'
          })
        ]
      }
    }, {
      loader: require.resolve('less-loader') // compiles Less to CSS
    }
  ]
}, {
  // 不匹配node_modules,theme的都不能自動(dòng)加載css-module
  test: /\.(css|less)$/,
  include: [/node_modules/,/theme/],
  use: [
    {
      loader: "style-loader"
    }, {
      loader: "css-loader",
      options: {
        importLoaders: 1
      }
    }, {
      loader: require.resolve('less-loader') // compiles Less to CSS
    }
  ]
},

使用: 在App.js中直接導(dǎo)入

import './assets/theme/App.less'

7-2. 熱更新

步驟一:

// 安裝react-hot-loader 
npm install --save-dev react-hot-loader

步驟二:

在webpack.config.js 的 entry 值里加上 react-hot-loader/patch

步驟三:

webpackDevServer.config.js中hot設(shè)置為true

步驟四: 在webpack.config.dev.js中在babel-loader中plugins加入react-hot-loader/babel

{
  test: /\.(js|jsx|mjs)$/,
  include: paths.appSrc,
  loader: require.resolve('babel-loader'),
  options: {
    // This is a feature of `babel-loader` for webpack (not Babel itself). It
    // enables caching results in ./node_modules/.cache/babel-loader/ directory for
    // faster rebuilds.
    cacheDirectory: true,
    plugins: [
      'react-hot-loader/babel'
    ]
  }
},

步驟五:

重寫index.js,App掛載

import { AppContainer } from 'react-hot-loader'

const render = Component => {
  ReactDOM.render(
    <AppContainer>
      <Component></Component>
    </AppContainer>,
    document.getElementById('root')
  )
}

render(App)

if(module.hot) {
  module.hot.accept('./App',() => {
    render(App);
  });
}

7-3. 本地瀏覽

直接在package.json中 加入

homepage:'.'

后記:使用react與vue的感悟

react是函數(shù)式編程,代碼難度、學(xué)習(xí)曲度、裝逼指數(shù),社區(qū)生態(tài)多樣性相比vue更高一點(diǎn)。

vue提供了大量的指令降低了開發(fā)難度,詳細(xì)完善的文檔,上手更快。

react提供較少的api,相比vue的指令,業(yè)務(wù)場(chǎng)景的功能需要自己實(shí)現(xiàn),難度更高一點(diǎn)

vue適合中小型項(xiàng)目,單兵、少量人員配合快速開發(fā)

react適合大型項(xiàng)目,多人協(xié)作

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • React從插槽、路由、redux的詳細(xì)過程

    React從插槽、路由、redux的詳細(xì)過程

    React需要自己開發(fā)支持插槽功能,原理:父組件組件中寫入的HTML,可以傳入子組件的props中,這篇文章主要介紹了React從插槽、路由、redux的詳細(xì)過程,需要的朋友可以參考下
    2022-10-10
  • React中Ref 的使用方法詳解

    React中Ref 的使用方法詳解

    這篇文章主要介紹了React中Ref 的使用方法,結(jié)合實(shí)例形式總結(jié)分析了react中ref基本功能、用法及操作注意事項(xiàng),需要的朋友可以參考下
    2020-04-04
  • React組件學(xué)習(xí)之Hooks使用

    React組件學(xué)習(xí)之Hooks使用

    這篇文章主要介紹了React hooks組件通信,在開發(fā)中組件通信是React中的一個(gè)重要的知識(shí)點(diǎn),本文通過實(shí)例代碼給大家講解react hooks中常用的父子、跨組件通信的方法,需要的朋友可以參考下
    2022-08-08
  • React模仿網(wǎng)易云音樂實(shí)現(xiàn)一個(gè)音樂項(xiàng)目詳解流程

    React模仿網(wǎng)易云音樂實(shí)現(xiàn)一個(gè)音樂項(xiàng)目詳解流程

    這篇文章主要介紹了React模仿網(wǎng)易云音樂實(shí)現(xiàn)一個(gè)音樂項(xiàng)目的詳細(xì)流程,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-08-08
  • React jsx轉(zhuǎn)換與createElement使用超詳細(xì)講解

    React jsx轉(zhuǎn)換與createElement使用超詳細(xì)講解

    這篇文章主要介紹了React jsx轉(zhuǎn)換與createElement使用,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧
    2022-11-11
  • 如何應(yīng)用?SOLID?原則在?React?中整理代碼之開閉原則

    如何應(yīng)用?SOLID?原則在?React?中整理代碼之開閉原則

    React?不是面向?qū)ο?,但這些原則背后的主要思想可能是有幫助的,在本文中,我將嘗試演示如何應(yīng)用這些原則來編寫更好的代碼,對(duì)React?SOLID原則開閉原則相關(guān)知識(shí)感興趣的朋友一起看看吧
    2022-07-07
  • 詳解使用React.memo()來優(yōu)化函數(shù)組件的性能

    詳解使用React.memo()來優(yōu)化函數(shù)組件的性能

    本文講述了開發(fā)React應(yīng)用時(shí)如何使用shouldComponentUpdate生命周期函數(shù)以及PureComponent去避免類組件進(jìn)行無用的重渲染,以及如何使用最新的React.memo API去優(yōu)化函數(shù)組件的性能
    2019-03-03
  • 詳解在React中跨組件分發(fā)狀態(tài)的三種方法

    詳解在React中跨組件分發(fā)狀態(tài)的三種方法

    這篇文章主要介紹了詳解在React中跨組件分發(fā)狀態(tài)的三種方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-08-08
  • React Component存在的幾種形式詳解

    React Component存在的幾種形式詳解

    這篇文章主要給大家介紹了關(guān)于React Component存在的幾種形式,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-11-11
  • React 組件渲染和更新的實(shí)現(xiàn)代碼示例

    React 組件渲染和更新的實(shí)現(xiàn)代碼示例

    這篇文章主要介紹了React-組件渲染和更新的實(shí)現(xiàn)代碼示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2019-02-02

最新評(píng)論

铜梁县| 托里县| 太仆寺旗| 修水县| 将乐县| 湘乡市| 深水埗区| 芦溪县| 济阳县| 嘉定区| 恩平市| 建宁县| 读书| 阿克苏市| 蓬安县| 婺源县| 廊坊市| 甘肃省| 喜德县| 白城市| 马关县| 辽阳县| 清镇市| 依兰县| 博白县| 台前县| 马山县| 香港 | 肥西县| 龙川县| 长海县| 双桥区| 凌源市| 武宣县| 锦州市| 资源县| 新绛县| 金门县| 兴山县| 宁河县| 手游|