javascript - How to add a module to my SystemJs config file so I can import it in angular - Stack Overflow

How do add new packages I just downloaded from npm to my Angular 2 ponents using SystemJS and using thi

How do add new packages I just downloaded from npm to my Angular 2 ponents using SystemJS and using this system.config.js file. 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. 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 ponents.

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 = [
        'mon',
        'piler',
        '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);

How do add new packages I just downloaded from npm to my Angular 2 ponents using SystemJS and using this system.config.js file. 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. 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 ponents.

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 = [
        'mon',
        'piler',
        '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);
Share Improve this question asked Jul 19, 2016 at 18:25 BathgateIOBathgateIO 3021 gold badge4 silver badges11 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 5

Just include this file into your main HTML file (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:

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

See this question (similar but for Lodash):

  • Importing lodash into app.ts file

Don't forget to install typings for the Underscore library.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744667449a4586836.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信