react-router4笔记
重要API一览路由容器组件
Route标签
Link标签
NavLink标签
Prompt标签
Redirect标签
match
import { BrowserRouter as Router,// 或者是HashRouter、MemoryRouter Route,// 这是基本的路由块 Link,// 这是a标签 Switch // 这是监听空路由的 Redirect // 这是重定向 Prompt // 防止转换 } from 'react-router-dom' 权限控制利用组件内的Redirect标签。 const PrivateRoute = ({ component: Component,...rest }) => ( <Route {...rest} render={props => ( fakeAuth.isAuthenticated ? ( <Component {...props}/> ) : ( <Redirect to={{ pathname: '/login',state: { from: props.location } }}/> ) )}/> ) 阻止离开当前路由在组件内部添加Prompt标签来进行权限控制。 <Prompt when={isBlocking} message={location => ( `你真的要跳转到 ${location.pathname}么?` )} /> 过渡动画样式分别定义:.example-enter、.example-enter.example-enter-active、.example-leave、.example-leave.example-leave-active。 <ReactCSSTransitionGroup transitionName="fade" transitionEnterTimeout={300} transitionLeaveTimeout={300} > <!-- 这里和使用 ReactCSSTransitionGroup 没有区别,唯一需要注意的是要把你的地址(location)传入「Route」里使它可以在动画切换的时候匹配之前的地址。 --> <Route location={location} key={location.key} path="/:h/:s/:l" component={HSL} /> </ReactCSSTransitionGroup> 按需加载官方方法借助 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'; // 引入单个页面(包括嵌套的子页面) // 同步引入 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',}, 个人觉得更好用的写法import Loadable from 'react-loadable' import Loading from './my-loading-component' Loadable({ loader: () => import(`views/AsyncView`),// 如果没有loading动画,就返回null LoadingComponent: () => null,// 如果有loading动画,则如下 loading: Loading }) webpack配置: // 添加插件babel-plugin-import-inspector { "plugins": [ ["import-inspector",{ "serverSideRequirePath": true,"webpackRequireWeakId": true,}] ] } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |