0
我正在使用react路由器v4在我的應用程序中路由。組件,傳奇和減速器是異步加載的。我現在如何在這種情況下實現私人路由和公共路由?作出反應路由器v4中的異步路由的私有路由
這裏是路由和負載
/**
* A wrapper component that will lazily render a component after it has been loaded.
*/
class Bundle extends Component {
static contextTypes = {
store: React.PropTypes.object
};
state = {
// short for "module" but that's a keyword in js, so "mod"
mod: null
};
componentWillMount() {
this.load(this.props);
}
/* istanbul ignore next */
componentWillReceiveProps(nextProps) {
if (nextProps.load !== this.props.load) {
this.load(nextProps);
}
}
load(props) {
this.setState({
mod: null
});
props.load(this.context.store, mod => {
this.setState({
// handle both es imports and cjs
mod: mod.default ? mod.default : mod
});
});
}
render() {
// eslint-disable-next-line no-unused-vars
const { load, ...otherProps } = this.props;
return this.state.mod && <this.state.mod {...otherProps} />;
}
}
const AsyncRoute = ({ load, ...others }) => (
<Route {...others} render={props => <Bundle load={load} {...props} />} />
);
AsyncRoute.propTypes = {
computedMatch: React.PropTypes.object,
path: React.PropTypes.string,
load: React.PropTypes.func
};
export default AsyncRoute;
// how can i make private route with different layout not a children of App
function Routes({ location }) {
return (
<Switch location={location}>
<AsyncRoute exact path="/" load={loadHomePage} />
<AsyncRoute exact path="/signup" load={loadSignupPage} />
<AsyncRoute path="" load={loadNotFoundPage} />
</Switch>
);
}
export default (store, cb) => {
const { injectReducer, injectSagas } = getAsyncInjectors(store);
const importModules = Promise.all([
import("./reducer"),
import("./sagas"),
import("./index")
]);
importModules.then(([reducer, sagas, component]) => {
injectReducer("signup", reducer.default);
injectSagas(sagas.default);
cb(component);
});
importModules.catch(errorLoading);
};
const render = messages => {
ReactDOM.render(
<Provider store={store}>
<ConnectedRouter history={history}>
<App />
</ConnectedRouter>
</Provider>,
document.getElementById("app")
);
};
你能告訴一瞥?我完全不理解。 – Serenity
添加了一些代碼草圖 –