简体   繁体   English

如何将模块添加到我的SystemJs配置文件,以便我可以角度导入它

[英]How to add a module to my SystemJs config file so I can import it in angular

How do add new packages I just downloaded from npm to my Angular 2 components using SystemJS and using this system.config.js file. 如何使用SystemJS将我刚刚从npm下载的新软件包添加到我的Angular 2组件并使用此system.config.js文件。 The code bellow was generated for me by a starter package. 下面的代码是由启动程序包为我生成的。 I have tried to put links to the modules in the map and packages section of this file but it doesn't seem to work. 我试图将链接放到该文件的map和packages部分中的模块,但它似乎不起作用。 All I want to know is how can I take a library such as underscore js located in my node_modules and input it into this file so I can easily import the file in my typescript angular components. 我想知道的是如何在我的node_modules中使用诸如下划线js之类的库并将其输入到该文件中,以便我可以轻松地在我的typescript角度组件中导入该文件。

var isPublic = typeof window != "undefined";

(function(global) {
    var map = {
        'app':                        'client', // 'dist',
        '@angular':                   (isPublic)? '@angular' : 'node_modules/@angular',
        '@angular/router':            (isPublic)? '@angular/router' : 'node_modules/@angular/router',
        'angular2-in-memory-web-api': (isPublic)? 'angular2-in-memory-web-api' : 'node_modules/angular2-in-memory-web-api',
        'rxjs':                       (isPublic)? 'rxjs' : 'node_modules/rxjs',
        'ng-semantic':                (isPublic)? 'ng-semantic' : 'node_modules/ng-semantic'
    };
    // packages tells the System loader how to load when no filename and/or no extension
    var packages = {
        'app':                        { main: 'main.js',  defaultExtension: 'js' },
        'rxjs':                       { defaultExtension: 'js' },
        'ng2-ace':                       { defaultExtension: 'js' },
        'angular2-in-memory-web-api': { main: 'index.js', defaultExtension: 'js' },
        'ng-semantic':                { main: 'ng-semantic', defaultExtension: 'js' }
    };
    var ngPackageNames = [
        'common',
        'compiler',
        'core',
        'http',
        'forms',
        'platform-browser',
        'platform-browser-dynamic',
        'router-deprecated',
        'upgrade',
        'ng2-ace'
    ];
    // Individual files (~300 requests):x
    function packIndex(pkgName) {
        packages['@angular/'+pkgName] = { main: 'index.js', defaultExtension: 'js' };
    }
    // Bundled (~40 requests):
    function packUmd(pkgName) {
        packages['@angular/'+pkgName] = { main: '/bundles/' + pkgName + '.umd.js', defaultExtension: 'js' };
    }

    packages['@angular/router'] = { main: 'index.js', defaultExtension: 'js' };


    var setPackageConfig = System.packageWithIndex ? packIndex : packUmd;
    // Add package entries for angular packages
    ngPackageNames.forEach(setPackageConfig);
    var config = {
        map: map,
        packages: packages
    };
    System.config(config);
})(this);

Just include this file into your main HTML file ( index.html ): 只需将此文件包含在主HTML文件( index.html )中:

<script src="node_modules/core-js/client/shim.min.js"></script>
<script src="node_modules/zone.js/dist/zone.js"></script>
<script src="node_modules/reflect-metadata/Reflect.js"></script>
<script src="node_modules/systemjs/dist/system.src.js"></script>

<script src="systemjs.config.js"></script> <-----

<script>
  System.import('app').catch(function(err){ console.error(err); });
</script>

Edit 编辑

You need to install the library using npm and configure it within the SystemJS configuration: 您需要使用npm安装库并在SystemJS配置中对其进行配置:

System.config({
  (...)
  map: {
    underscore: 'node_modules/underscore/underscore.js'
  }
});

See this question (similar but for Lodash): 看到这个问题(类似但是对于Lodash):

Don't forget to install typings for the Underscore library. 不要忘记为Underscore库安装打字。

暂无
暂无

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

相关问题 如何导入配置文件,以便在Jest测试下通过构建的bundle和模块找到它? - How to import a config file so it can be found both by built bundle and module under Jest test? 如何使用SystemJs导入节点模块? - How to import node module with SystemJs? 如何添加 UMD 以便我可以在浏览器中使用我的模块并将其用作 NPM 模块? - How can I add UMD so I can use my module in the browser and as an NPM module? 如何正确导入配置文件? - How can I import the config file properly? 如何使用 jest 配置全局导入模块? - How can I import a module globally using jest config? 如何在一个配置中为模板文件设置路径,例如requireJs中的文件,这样我就不必将模板路径URL传递给我的模块了? - How to set paths for template files in one config like file in requireJs so that I do not have to pass template path url to my module? 如何在Angular 4 / Ionic中的组件中正确导入管道模块? - How can I properly import a Pipe Module in my Component in Angular 4/Ionic? WebStorm在使用SystemJS模块系统时从索引文件导入 - WebStorm to import from index file when using SystemJS module system (Angular2 RC5)如何使用可以由路由器延迟加载的SystemJS为每个模块创建一个包? - (Angular2 RC5) How to create a bundle for each module using SystemJS that can be lazily loaded by the router? 使用 systemjs 模块导入 navigo - Import navigo using systemjs module
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM