Requirejs回调在代码拆分时未定义

问题描述 投票:0回答:1

我是新手,要求我有一个以index.jsx为入口点的React应用程序

 // index.jsx

 import React from 'react';
 import ReactDOM from 'react-dom';


 export function callBackForRequirejs(){
    return "testing"
 }

当我通过requestJs加载我的构建时,我得到了这些回调

    require(["/path/to/bundle"], function(callback) {
      console.log(callback) // I get "callBackForRequirejs"
    }, function(err){
      console.log(err)
    });

但是当我进行代码拆分时,我在回调中变得未定义,对于代码拆分,我正在使用这些配置

    optimization: {
       splitChunks: {
           cacheGroups: {
               commons: {
                   test: /[\\/]node_modules[\\/]/,
                   name: "vendor",
                   chunks: "initial",
               }
           }
       }
   } 
reactjs requirejs code-splitting
1个回答
0
投票

看来,用于代码拆分的方法是使用webpack。 webpack并要求js不能真正相处。

您应该尝试使用Vanilla JS。.

<script onload="handleOnLoad()" />

或购买npm软件包。react-load-script - npm

© www.soinside.com 2019 - 2024. All rights reserved.