简体   繁体   English

如何从嵌套模块导入?

[英]How to import from nested module?

I have a Typescript library I've written which is being emitted as follows. 我有一个我编写的Typescript库,它的发出方式如下。 I'm not sure how to import from this module, though. 不过,我不知道如何从这个模块导入。

node_modules/my-module/dist/index.js (partial) node_modules / my-module / dist / index.js (部分)

define("services/UserService", ["require", "exports", ..., "services/BaseService"], function (require, exports, ..., BaseService_31) {
    "use strict";
    var UserService = (function (_super) {
        // ... stuff here ...

        return UserService;
    }(BaseService_31.BaseService));
    exports.UserService = UserService;
});

I've installed this package with npm and configured JSPM as follows: 我已经用npm安装了这个包并配置了JSPM,如下所示:

config.js (partial) config.js (部分)

System.config({
  defaultJSExtensions: true,
  transpiler: "none",
  paths: {
    "*": "dist/*",
    "github:*": "jspm_packages/github/*",
    "npm:*": "jspm_packages/npm/*",
    "npm-ext:*": "node_modules/*"
  },
  map: {
    ...
    "my-module": "npm-ext:my-module/dist/index.js",
    ...
  }
});

I was expecting to be able to import this class as follows... 我期待能够导入这个类如下...

import {UserService} from "my-module/services/UserService";

I expected SystemJS to resolve the path to my-module and then locate the services/UserService module, and grab the single export UserService . 我希望SystemJS解析my-module的路径,然后找到services/UserService模块,并获取单个导出UserService But in the Chrome Console I see this is the path which is being loaded: 但在Chrome控制台中,我看到这是正在加载的路径:

node_modules/my-module/dist/index.js/models/UserService.js

What's the correct way to import a module such as this? 导入这样的模块的正确方法是什么?

Bonus: how can I get around including the full path to index.js ? 额外奖励:我如何解决包括index.js的完整路径?

Your library code was emitted using named define 您的库代码是使用命名定义发出的

define("services/UserService",

And it looks like there are several such modules defined in the index.js file, that is, index.js is a bundle. 看起来index.js文件中定义了几个这样的模块,也就是说,index.js是一个bundle。

The only way to import such module using SystemJS is 使用SystemJS导入此类模块的唯一方法是

import {UserService} from "services/UserService";

That is, imported module name must match exactly the name given to define . 也就是说,进口的模块名称必须给予名称匹配define In your case, the location of the file does not affect module name in any way. 在您的情况下,文件的位置不会以任何方式影响模块名称。

Now, SystemJS has to be configured to load library file, node_modules/my-module/dist/index.js , when it sees that import. 现在,SystemJS必须配置为在看到导入时加载库文件node_modules/my-module/dist/index.js For bundles, the preferred way to to it is via bundles config : 对于bundle,首选方法是通过bundle配置

  bundles: {
    "npm-ext:my-module/dist/index.js": ["services/UserService.js"]
  }

Module name here, services/UserService.js , must have .js extension because it must match imported module name after defaultJSExtesions:true is applied. 这里的模块名称services/UserService.js必须具有.js扩展名,因为在应用defaultJSExtesions:true后它必须匹配导入的模块名称。

If you want the module to be imported as my-module/services/UserService.js , then you need to make sure it is registered with that name. 如果要将模块导入为my-module/services/UserService.js ,则需要确保使用该名称注册该模块。 The easiest way is to have corresponding source file structure when compiling the library - that is, have UserService.ts in my-module/services folder. 最简单的方法是在编译库时拥有相应的源文件结构 - 也就是说,在my-module/services文件夹中有UserService.ts。

You can find more information about named define in SystemJS module format docs , which says in particular 您可以在SystemJS模块格式docs中找到有关命名define的更多信息,特别是说

  • Named defines are supported and will write directly into the loader registry. 支持命名定义,并将直接写入loader注册表。

  • A single named define will write into the loader registry but also be treated as the value of the module loaded if the names do not match. 单个命名的define将写入loader注册表,但如果名称不匹配,也会被视为加载的模块的值。 This enables loading a module containing define('jquery', .... 这样就可以加载一个包含define的模块('jquery',....

PS Your browser tries to load PS您的浏览器尝试加载

node_modules/my-module/dist/index.js/models/UserService.js

where does models come from? models来自哪里?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM