代码之家  ›  专栏  ›  技术社区  ›  Carlos Muentes

使用SystemJS导入Angular 2中的应用程序模块

  •  0
  • Carlos Muentes  · 技术社区  · 9 年前

    我正在使用SystemJS开发Angular 2和Typescript项目。现在,我发现我们经常不得不做这样的事情 ../../../ '根据我们在给定组件中的嵌套深度从其他模块加载组件。似乎使用SystemJS的映射设置可以让我在不使用可怕的相对路径方法的情况下导入应用程序范围的模块,但我无法确定是否可以这样做。我也对解决这个问题的其他方法持开放态度。

    基本上我想做的是这样的:

    import {Component} from '@angular/core';
    import {SecureHttpClient} from 'mySecurityModule'; //No ../../../
    
    @Component({
      moduleId: module.id,
      selector: 'my-selector',
      templateUrl: 'my.component.html'
    })
    export class MyComponent {
    
      constructor(private client: SecureHttpClient) {
    
      }
    }
    

    我尝试了下面的配置,但不起作用(TS编译失败)。

    (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',
          '@angular/upgrade': 'npm:@angular/upgrade/bundles/upgrade.umd.js',
    
    
          // other libraries
          'rxjs':                      'npm:rxjs',
          'angular-in-memory-web-api': 'npm:angular-in-memory-web-api',
          '@ng-bootstrap/ng-bootstrap': 'npm:@ng-bootstrap/ng-bootstrap/bundles/ng-bootstrap.js',
    
          // Internal Modules
          'mySecurityModule': 'app/secure/secure.module'
    
        },
        // 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'
          },
          'angular-in-memory-web-api': {
            main: './index.js',
            defaultExtension: 'js'
          }
        }
      });
    })(this);
    

    我知道可能还有其他方法可以做到这一点,但我还是找不到任何明确的方法,这是我非常想解决的问题。再次感谢!

    1 回复  |  直到 9 年前
        1
  •  0
  •   Sasxa    9 年前

    您可以将typescript编译器设置为使用基本URL并从那里导入。在里面 tsconfig.json 添加:

      "compilerOptions": {
        "baseUrl": "./",
        ...
      }
    

    然后,您可以使用app root作为绝对路径。差不多

    import {SecureHttpClient} from 'app/mySecurityModule';
    

    而不是:

    import {SecureHttpClient} from '../../../mySecurityModule';
    

    实际路径可能与您不同。

    然而,我建议为编辑器使用某种导入扩展(例如, AutoImport 对于VSCode),因为某些工具(AoT、CLI)可能存在此问题。希望随着工具越来越成熟,情况会有所改善(;

    推荐文章