webpac2中使用import加载模块取消babel配置出现错误

发布于 2022-09-04 11:58:21 字数 754 浏览 8 评论 0

function loadRoute(cb,name){
        return (module) => cb(null,module[name]);
    }
    
    
           <Router history={hashHistory} >
                    <Route path="/" component={Unique_main}>
                        <IndexRoute component={main_pro} />
                        <Route path="people" getComponent={(location, cb)=> {
                            console.log(location);
                            System.import('./components/people/people.jsx')
                                .then(()=>loadRoute(cb,"default"))
                                .catch(errorLoading);
                        }}></Route>
                    </Route>
           </Router> 
       

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

手心的温暖 2022-09-11 11:58:21
/**
 * Created by apple on 16/9/13.
 */
import React, { Component, PropTypes } from 'react';
import { Route, IndexRoute, withRouter, browserHistory } from 'react-router';
import { Home } from './component/home';
import { Detail } from './component/detail';
import { valid_user } from './api/auth';
import Helmet from 'react-helmet';


// 无状态(stateless)组件,一个简单的容器,react-router 会根据 route
// 规则匹配到的组件作为 `props.children` 传入
const Container = (props) => {
  return (
    <div>
      <Helmet title="React Application Demonstration"/>
      {props.children}
    </div>
  );
};

const isReactComponent = (obj) => Boolean(obj && obj.prototype && Boolean(obj.prototype.isReactComponent));

function errorLoading(err) {
  console.error('Dynamic page loading failed', err);
}


function loadRoute(cb) {
  return (module) => cb(null, module.Login);
}

/**
 *
 * @param store
 */
export default (store = {}) => {

  //注意,在SSR情况下这里使用的是服务端传入的Store

  /**
   * @function 判断用户是否登陆,如果未登陆则强制性跳转到登录页面
   * @param nextState
   * @param replace
   * @param callback
   */
  async function auth(nextState, replace, callback) {

    let userToken = store.userToken;

    //在这里执行异步认证,假设传入的store中包含userToken
    //这里使用Promise执行异步操作
    //如果是SSR,则本部分代码会在服务端运行

    let isValid = await valid_user(userToken);

    //如果用户尚未认证,则进行跳转操作
    isValid || replace('/login');

    //执行回调函数
    callback();

  }

  // route 规则:
  // - `/list` 显示 `List` 组件
  // - `/item/:id` 显示 `Item` 组件
  return (
    <Route path="/" history={browserHistory} component={Container}>
      <IndexRoute component={Home}/>
      <Route path="home" component={withRouter(Home)}/>
      <Route path="login" getComponent={
        (location, cb)=> {
          console.log(location);
          System.import('./component/login')
            .then(loadRoute(cb, false))
            .catch(errorLoading);
          }
      }/>
      <Route path="detail" component={withRouter(Detail)} onEnter={auth}/>
    </Route>
  );

}


~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文