react+antd元件做一個完整的後台(附程式碼)
這次帶給大家react antd元件做一個完整的後台(附程式碼),react antd元件做後台的注意事項有哪些,下面就是實戰案例,一起來看一下。
前言:此文需要有一定react,redux基礎,具體學習資料請科學上網。
使用create-react-app腳手架
具體基礎配置請參考
配合antd元件實現的管理系統demo,線上位址
開發前反思
1. 按需載入
webpack的import 動態載入的模組的函數,import(參數),參數為模組位址。
注意: import 後會回傳一個promise物件。
import('/components/chart').then(mud => { dosomething(mod) });
本demo建構了非同步載入元件Bundle,具體程式碼請見
class Bundle extends Component { constructor(props) { super(props); this.state = { mod: null }; } unmount = false componentDidMount = () => { // 加载组件时,打开全局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对象; mod.default导出默认 props.load().then((mod) => { if (this.unmount) { // 离开组件时,不异步执行setState this.props.dispatch(loading(false)) return false } this.setState({ mod: mod.default ? mod.default : mod }, _ => { // 组件加载完毕,关闭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 /> : <p>加载中...</p> }} </Bundle>
2. 全域loading
#配合redux,dispatch => reducer更新=> mapstate更新,在根元件進行loading的渲染
#詳細請見本demo位址src/routers/router.js-render函數
3. 設定路由物件
專案佈局如下
#本demo使用的是router4,官方文件示範為單行Route(如vue種的router),未有統一配置物件。管理系統基本上圍繞著content進行業務開發,建構通用配置有助於開發建構router.config.js
const routers = [ { menuName: '主页', menuIco: 'home', component: 'home/home.js', // 主页 path: '/admin/home' // 主页 }, { menuName: '用户', menuIco: 'user', children: [ { menuName: '用户列表', component: 'user/list.js', // 主页 path: '/admin/user/list' // 主页 } ] }, { menuName: '多级菜单', menuIco: 'setting', children: [ { menuName: '多级菜单2', children: [ { menuName: '菜单', component: 'user/list.js', // 主页 path: '/admin/user/list3' // 主页 } ] } ] }, { menuName: '关于我', menuIco: 'smile-o', component: 'about/about.js', // 主页 path: '/admin/about' // 主页 } ]
#實現思路,最外層佈局為Admin,content被Admin包裹,那麼可以利用this. props.children ,把內容打入content。 (利用bundle元件非同步載入後塞入元件進行渲染)
<Admin> <Content { ...this.props } breadcrumb={this.state.breadcrumb}> {this.props.children} </Content> </Admin> // Content组件内部 render() { return ( <p> {this.props.children} </p> ) } // 本demo实现,详见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
多人配合開發,一些業務場景的元件需要狀提升(不理解狀態提升的同學,請科學上網)
import otherReducers from './otherReducers' const App = combineReducers({ rootReducers, ...otherReducers // 其他需要增加的reducers })
5. 登陸驗證
利用withRouter 函數,頁面進行路由跳轉時觸發該函數
const newWithRouter = withRouter(props => { // .... })
若未登錄,則回傳
return <Redirect to="/login" />
6. 路由攔截
同上,依照路由配置與權限,傳回對應的選單或屏蔽
return <Redirect to={其他} />
7 其他設定
7-1. 自訂樣式
// 修改webpack.config.dev.js 和 webpack.config-prod.js 配置文件 { test: /\.(css|less)$/, // 匹配src的都自动加载css-module include: [/src/], exclude: [/theme/], use: [ require.resolve('style-loader'), { loader: require.resolve('css-loader'), options: { importLoaders: 1, modules: true, // 新增对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的都不能自动加载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中直接匯入
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設定為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是函數式編程,代碼難度、學習曲度、裝逼指數,社區生態多樣性相比vue更高一點。
vue提供了大量的指令降低了開發難度,詳細完善的文檔,上手更快。
react提供較少的api,相比vue的指令,業務場景的功能需要自己實現,難度更高一點
vue適合中小型項目,單兵、少量人員配合快速開發
react適合大型項目,多人協作
相信看了本文案例你已經掌握了方法,更多精彩請關注php中文網其它相關文章!
推薦閱讀:
以上是react+antd元件做一個完整的後台(附程式碼)的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

React前後端分離指南:如何實現前後端的解耦和獨立部署,需要具體程式碼範例在當今的Web開發環境中,前後端分離已經成為一種趨勢。透過將前端和後端程式碼分開,可以讓開發工作更加靈活、高效,並且方便進行團隊協作。本文將介紹如何使用React實現前後端分離,從而實現解耦和獨立部署的目標。首先,我們要先理解什麼是前後端分離。傳統的Web開發模式中,前端和後端是耦合在

如何利用React和RabbitMQ建立可靠的訊息傳遞應用程式引言:現代化的應用程式需要支援可靠的訊息傳遞,以實現即時更新和資料同步等功能。 React是一種流行的JavaScript庫,用於建立使用者介面,而RabbitMQ是一種可靠的訊息傳遞中間件。本文將介紹如何結合React和RabbitMQ建立可靠的訊息傳遞應用,並提供具體的程式碼範例。 RabbitMQ概述:

ReactRouter使用指南:如何實現前端路由控制隨著單頁應用的流行,前端路由成為了一個不可忽視的重要部分。 ReactRouter作為React生態系統中最受歡迎的路由庫,提供了豐富的功能和易用的API,使得前端路由的實作變得非常簡單和靈活。本文將介紹ReactRouter的使用方法,並提供一些具體的程式碼範例。安裝ReactRouter首先,我們需要

PHP、Vue和React:如何選擇最適合的前端框架?隨著互聯網技術的不斷發展,前端框架在Web開發中起著至關重要的作用。 PHP、Vue和React作為三種代表性的前端框架,每一種都具有其獨特的特徵和優勢。在選擇使用哪種前端框架時,開發人員需要根據專案需求、團隊技能和個人偏好做出明智的決策。本文將透過比較PHP、Vue和React這三種前端框架的特徵和使

Java框架與React框架的整合:步驟:設定後端Java框架。建立專案結構。配置建置工具。建立React應用程式。編寫RESTAPI端點。配置通訊機制。實戰案例(SpringBoot+React):Java程式碼:定義RESTfulAPI控制器。 React程式碼:取得並顯示API回傳的資料。

如何利用React開發一個響應式的後台管理系統隨著互聯網的快速發展,越來越多的企業和組織需要一個高效、靈活、易於管理的後台管理系統來處理日常的操作事務。 React作為目前最受歡迎的JavaScript庫之一,提供了一種簡潔、高效和可維護的方式來建立使用者介面。本文將介紹如何利用React開發一個響應式的後台管理系統,並給出具體的程式碼範例。建立React專案首先

如何建立適用於學校管理系統的MySQL表結構?學校管理系統是一個涉及多個模組和功能的複雜系統,為了實現其功能需求,需要設計合適的資料庫表結構以儲存資料。本文將以MySQL為例,介紹如何建立適用於學校管理系統的表格結構,並提供相關的程式碼範例。學校資訊表(school_info)學校資訊表用於儲存學校的基本訊息,如學校名稱、地址、聯絡電話等。 CREATETABL

Vue.js適合中小型項目和快速迭代,React適用於大型複雜應用。 1)Vue.js易於上手,適用於團隊經驗不足或項目規模較小的情況。 2)React的生態系統更豐富,適合有高性能需求和復雜功能需求的項目。
