【问题标题】:Can't import bundled file无法导入捆绑文件
【发布时间】:2015-12-16 20:40:10
【问题描述】:

我在我的应用程序中这样做

System.import('lib/bootstrap.js').then(m => {
   this.socket = m.io("http://localhost:3000");
})

这是bootstrap.js

import io from 'socket.io-client';
export { io };

我通过jspm bundle lib/bootstrap.js outfile.js 创建了一个捆绑包。

当我尝试System.import('outfile.js') 时,已解决的 Promise m 只是一个空对象。 我在这里做错了什么?

System.import('outfile.js').then(m => {
   this.socket = m.io("http://localhost:3000");
})

【问题讨论】:

    标签: javascript ecmascript-6 systemjs jspm es6-module-loader


    【解决方案1】:

    您不想导入捆绑的文件。您需要做的是将捆绑配置注入到您的 config.js 文件中。例如添加jspm bundle lib/bootstrap bootstrap-bundle.js --inject 将添加

    "bundles": {
        "bootstrap-bundle": [
        "socket.io-client.js",
        "lib/bootstrap.js"
      ]
    }
    

    到您的 config.js 文件。然后你只需要像往常一样导入你的文件:

    System.import('lib/bootstrap.js').then(m => {
       this.socket = m.io("http://localhost:3000");
    })
    

    请参阅documentation here

    【讨论】:

    • 如果我在捆绑之后仍然加载 lib/bootstrap.js,那么捆绑有什么好处呢?理想情况下,我只想加载 system.Js,然后再加载一个文件。
    • 您不只是加载文件。您加载所有捆绑包。问题是 jspm 足够聪明,可以为您要求的文件加载包。例如,在您的示例中,您将加载具有 2 个依赖项的包。如果您不使用捆绑,则需要单独加载它们(即对服务器的两个请求)。
    猜你喜欢
    • 1970-01-01
    • 2020-11-10
    • 2018-04-01
    • 2017-04-22
    • 2017-01-31
    • 2019-01-02
    • 1970-01-01
    • 1970-01-01
    • 2019-07-16
    相关资源
    最近更新 更多