詳解react-router4 異步加載路由兩種方法
方法一:我們要借助bundle-loader來實(shí)現(xiàn)按需加載。
首先,新建一個(gè)bundle.js文件:
import React, { Component } from 'react'
export default class Bundle extends React.Component {
state = {
// short for "module" but that's a keyword in js, so "mod"
mod: null
}
componentWillMount() {
this.load(this.props)
}
componentWillReceiveProps(nextProps) {
if (nextProps.load !== this.props.load) {
this.load(nextProps)
}
}
load(props) {
this.setState({
mod: null
})
props.load((mod) => {
this.setState({
// handle both es imports and cjs
mod: mod.default ? mod.default : mod
})
})
}
render() {
if (!this.state.mod)
return false
return this.props.children(this.state.mod)
}
}
然后,在入口處使用按需加載:
// ...
// bundle模型用來異步加載組件
import Bundle from './bundle.js';
// 引入單個(gè)頁面(包括嵌套的子頁面)
// 同步引入
import Index from './app/index.js';
// 異步引入
import ListContainer from 'bundle-loader?lazy&name=app-[name]!./app/list.js';
const List = () => (
<Bundle load={ListContainer}>
{(List) => <List />}
</Bundle>
)
// ...
<HashRouter>
<Router basename="/">
<div>
<Route exact path="/" component={Index} />
<Route path="/list" component={List} />
</div>
</Router>
</HashRouter>
// ...
webpack.config.js文件配置
output: {
path: path.resolve(__dirname, './output'),
filename: '[name].[chunkhash:8].bundle.js',
chunkFilename: '[name]-[id].[chunkhash:8].bundle.js',
},
方法二:用原生的
Webpack 配置
首先在 webpack.config.js 的 output 內(nèi)加上 chunkFilename
output: {
path: path.join(__dirname, '/../dist/assets'),
filename: 'app.js',
publicPath: defaultSettings.publicPath,
// 添加 chunkFilename
chunkFilename: '[name].[chunkhash:5].chunk.js',
},
name 是在代碼里為創(chuàng)建的 chunk 指定的名字,如果代碼中沒指定則 webpack 默認(rèn)分配 id 作為 name。
chunkhash 是文件的 hash 碼,這里只使用前五位。
在路由頁面
這里寫的是舊的沒按需要加載的路由寫法
ReactDOM.render(
(
<Router history={browserHistory}>
{/* 主頁 */}
<Route path="/" component={App}>
{/* 默認(rèn) */}
<IndexRoute component={HomePage} />
{/* baidu */}
<Route path="/baidu" component={BaiduPage}>
<Route path="result" component={BaiduResultPage} />
<Route path="frequency" component={BaiduFrequencyPage} />
</Route>
{/* 404 */}
<Route path='/404' component={NotFoundPage} />
{/* 其他重定向到 404 */}
<Redirect from='*' to='/404' />
</Route>
</Router>
), document.getElementById('app')
);
我們需要讓路由動(dòng)態(tài)加載組件,需要將 component 換成 getComponent
ReactDOM.render(
(
<Router history={browserHistory}>
{/* 主頁 */}
<Route path="/" component={App}>
{/* 默認(rèn) */}
<IndexRoute component={HomePage} />
{/* baidu */}
<Route path="/baidu"
getComponent(nextState, cb)
{ require.ensure([], (require) => { cb(null, require('components/layer/HomePage')) }, 'HomePage')}>
<Route path="result" getComponent... />
<Route path="frequency" getComponent... />
</Route>
{/* 404 */}
<Route path='/404' getComponent... />
{/* 其他重定向到 404 */}
<Redirect path='*' onEnter: (_, replaceState) => replaceState(null, "/404") />
</Route>
</Router>
), document.getElementById('app')
);
getComponent
對應(yīng)于以前的 component 屬性,但是這個(gè)方法是異步的,也就是當(dāng)路由匹配時(shí),才會(huì)調(diào)用這個(gè)方法。
這里面有個(gè) require.ensure 方法
require.ensure(dependencies, callback, chunkName)
這是 webpack 提供的方法,這也是按需加載的核心方法。第一個(gè)參數(shù)是依賴,第二個(gè)是回調(diào)函數(shù),第三個(gè)就是上面提到的 chunkName,用來指定這個(gè) chunk file 的 name。
如果需要返回多個(gè)子組件,則使用 getComponents 方法,將多個(gè)組件作為一個(gè)對象的屬性通過 cb 返回出去即可。這個(gè)在官方示例也有,但是我們這里并不需要,而且根組件是不能返回多個(gè)子組件的,所以使用 getComponent。
當(dāng)改寫之后,我們需要把這個(gè)重定向的路由單獨(dú)拆出來,也就是 * 這個(gè)路由,我們上面已經(jīng)為他創(chuàng)建了一個(gè) redirect 目錄。這里使用到 onEnter 方法,然后在這個(gè)方法里改變路由狀態(tài),調(diào)到另外的路由,實(shí)現(xiàn) redirect :
/redirect/index.js
module.exports = {
path: '*',
onEnter: (_, replaceState) => replaceState(null, "/404")
}
The root route must render a single element
跟著官方示例和上面碼出來之后,可能頁面并沒有渲染出來,而是報(bào) The root route must render a single element 這個(gè)異常,這是因?yàn)?module.exports 和 ES6 里的 export default 有區(qū)別。
如果你是使用 es6 的寫法,也就是你的組件都是通過 export default 導(dǎo)出的,那么在 getComponent 方法里面需要加入.default。
getComponent(nextState, cb) {
require.ensure([], (require) => {
// 在后面加 .default
cb(null, require('components/layer/ReportPage')).default
}, 'ReportPage')
}
如果你是使用 CommonJS 的寫法,也就是通過 module.exports 導(dǎo)出的,那就無須加 .default 了。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
react搭建在線編輯html的站點(diǎn)通過引入grapes實(shí)現(xiàn)在線拖拉拽編輯html
Grapes插件是一種用于Web開發(fā)的開源工具,可以幫助用戶快速創(chuàng)建動(dòng)態(tài)和交互式的網(wǎng)頁元素,它還支持多語言和多瀏覽器,適合開發(fā)響應(yīng)式網(wǎng)頁和移動(dòng)應(yīng)用程序,這篇文章主要介紹了react搭建在線編輯html的站點(diǎn)通過引入grapes實(shí)現(xiàn)在線拖拉拽編輯html,需要的朋友可以參考下2023-08-08
react-router JS 控制路由跳轉(zhuǎn)實(shí)例
這篇文章主要介紹了react-router JS 控制路由跳轉(zhuǎn)實(shí)例,react實(shí)現(xiàn)路由可以直接使用react-router。有興趣的可以了解一下2017-06-06
詳解使用webpack+electron+reactJs開發(fā)windows桌面應(yīng)用
這篇文章主要介紹了詳解使用webpack+electron+reactJs開發(fā)windows桌面應(yīng)用,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-02-02
React函數(shù)組件傳參的實(shí)現(xiàn)
React函數(shù)組件通過接受props實(shí)現(xiàn)組件間的數(shù)據(jù)傳遞,通過組件標(biāo)簽的屬性向子組件傳遞數(shù)據(jù),并在子組件中通過參數(shù)接收,還可以使用ES6的解構(gòu)賦值,函數(shù)也能作為props傳遞,以實(shí)現(xiàn)父子組件間的交互和通信,下面就來具體了解一下2024-09-09
ReactNative實(shí)現(xiàn)Toast的示例
這篇文章主要介紹了ReactNative實(shí)現(xiàn)Toast的示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-12-12
Redux?Toolkit的基本使用示例詳解(Redux工具包)
這篇文章主要介紹了Redux?Toolkit的基本使用,本文結(jié)合示例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-12-12

