在Angular 2中,system.config.js文件有什么用途?

8

var map, packages和var config分别是什么意思?请解释一下map和package对象的所有配置属性。是否有可用的配置文档?

这是我的系统配置文件:

/**
 * System configuration for Angular samples
 * Adjust as necessary for your application needs.
 */
(function (global) {
  System.config({
    paths: {
      // paths serve as alias
      'npm:': 'node_modules/'
    },
    // map tells the System loader where to look for things
    map: {
      // our app is within the app folder
      app: 'app',

      // angular bundles
      '@angular/core': 'npm:@angular/core/bundles/core.umd.js',
      '@angular/common': 'npm:@angular/common/bundles/common.umd.js',
      '@angular/compiler': 'npm:@angular/compiler/bundles/compiler.umd.js',
      '@angular/platform-browser': 'npm:@angular/platform-browser/bundles/platform-browser.umd.js',
      '@angular/platform-browser-dynamic': 'npm:@angular/platform-browser-dynamic/bundles/platform-browser-dynamic.umd.js',
      '@angular/http': 'npm:@angular/http/bundles/http.umd.js',
      '@angular/router': 'npm:@angular/router/bundles/router.umd.js',
      '@angular/forms': 'npm:@angular/forms/bundles/forms.umd.js',

      // other libraries
      'rxjs': 'npm:rxjs',
      'angular-in-memory-web-api': 'npm:angular-in-memory-web-api/bundles/in-memory-web-api.umd.js',
      'fscopy': 'npm:fs-extra/lib/copy/index.js',
      'file-system': 'npm:file-system/file-system.js'
    },
    // packages tells the System loader how to load when no filename and/or no extension
    packages: {
      app: {
        main: './main.js',
        defaultExtension: 'js'
      },
      rxjs: {
        defaultExtension: 'js'
      },
      fs: {
        defaultExtension: 'js'
      }
    }
  });
})(this);
3个回答

12

我想跟进@Sajeetharan的回答,并给出一个详细的例子。假设你想安装一个新的模块,我们将以angular2-highcharts为例。参考资料在hightcharts文档中。

  1. 你需要运行npm命令npm install angular2-highcharts --save来开始安装模块。

    a. 现在你将在node_modules文件夹中看到已安装的模块。

  2. 好了,现在你已经安装了一个新的模块以供使用,接下来你需要告诉你的应用程序在哪里找到这个新模块并如何加载它。这就是你需要使用systemjs.config.js的地方。

    a. 首先,你需要"map"或告诉你的应用程序在哪里找到这个新的模块。在这种情况下,它看起来像这样...'angular2-highcharts':'node_modules/angular2-highcharts',

    1. 现在让我们简单解释一下。 'angular2-highcharts'表示如果你引用angular2-highcharts,则使用以下路径'node_modules/angular2-highcharts'

    b. 接下来是Package部分。现在我们要说的是,在映射到新模块的位置之后,你想运行这个新模块文件夹中的哪些内容?在这种情况下,它是`index.js',我们可以这样定义...

    angular2-highcharts': {
      main: './index.js',
      defaultExtension: 'js'
    }
    

    现在你已经正确安装了该模块并在您的systemjs.config.js文件中引用了它,你可以在你的'app.modules'组件中以及任何你希望的组件中调用该模块。

    编辑

    忘记解释“config”了。Config只是一种使用简写值定义文件或文件夹的方式。在您的配置中,npm:node_modules,基本上是说您可以用npm代替node_modules。这在您的映射语句中显示如下:'npm:@angular/core/bundles/core.umd.js',而不是写出node_modules/@angular/core/bundles/core.umd.js


2

system.config.js是允许加载使用TypeScript编译器编译的模块(node modules)的文件之一。map指的是模块名称与包含JavaScript代码的JS文件之间的对应关系。


1
如果您正在使用Angular-Cli,则不需要systemjs.config。一切都应该由angular-cli处理。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接