如何在Angular 2中导入其他模块?

如何在Angular 2中导入其他模块?,angular,Angular,我试图在Angular2应用程序中使用Angular2/http模块,但当我尝试导入它时,system.src.js会抛出一个错误http:///angular2/http 404找不到 下面是my index.html的外观: <html> <head> <title>Angular 2 QuickStart</title> <!-- 1. Load libraries --> <script sr

我试图在Angular2应用程序中使用Angular2/http模块,但当我尝试导入它时,system.src.js会抛出一个错误http:///angular2/http 404找不到

下面是my index.html的外观:

<html>
  <head>
    <title>Angular 2 QuickStart</title>

    <!-- 1. Load libraries -->
    <script src="angular2/bundles/angular2-polyfills.js"></script>
    <script src="systemjs/dist/system.src.js"></script>

    <script src="rxjs/bundles/Rx.js"></script>
    <script src="angular2/bundles/angular2.dev.js"></script>
    <!-- 2. Configure SystemJS -->
    <script>
      System.config({
        packages: {
          app: {
            format: 'register',
            defaultExtension: 'js'
          }
        }
      });
      System.import('app/boot')
            .then(null, console.error.bind(console));
    </script>

    <link href="bootstrap/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="font-awesome/css/font-awesome.min.css" rel="stylesheet">
    <link href="assets/styles/main.css" rel="stylesheet">
  </head>
  <!-- 3. Display the application -->
  <body>
    <my-app>Loading...</my-app>
  </body>
</html>
我尝试导入http的组件:

import {Injectable} from 'angular2/core';
import {Track} from './track'
import {Http, Headers} from 'angular2/http';

@Injectable()
export class ActionTracker {
  constructor(private _http : Http){}

  login(){
    this._http.post('/api/login', "")
    .map(res => res.json())
    .subscribe(
      data => console.log(data),
      err => console.log(err),
      () => console.log('Authentication Complete')
    );
  }
}
如果有人能解释一下如何进行导入,那就太好了。在我失败之前,我还尝试导入第三方库


谢谢您的帮助。

在index.html中包含http.dev.js

<script src="angular2/bundles/http.dev.js"></script>

在index.html中包含http.dev.js

<script src="angular2/bundles/http.dev.js"></script>

您必须将其包含在脚本标记中或使用SystemJS加载

如果你要通过Stjjs来做这件事,请考虑这个要点:

基本上,您使用映射或路径映射其他库,然后在应用程序中导入它们。如果在脚本标记中包含angular2.min.js和http.min.js,则不需要使用SystemJS(SystemJS的优点是能够在最后将其捆绑到一个文件中)

例如:

如果在地图中执行此操作:

'my-library': 'location/my-libary.js'
在您的代码中,您正在使用以下内容导入库:

import * as my-library from 'my-library'


您必须将其包含在脚本标记中或使用SystemJS加载它

如果你要通过Stjjs来做这件事,请考虑这个要点:

基本上,您使用映射或路径映射其他库,然后在应用程序中导入它们。如果在脚本标记中包含angular2.min.js和http.min.js,则不需要使用SystemJS(SystemJS的优点是能够在最后将其捆绑到一个文件中)

例如:

如果在地图中执行此操作:

'my-library': 'location/my-libary.js'
在您的代码中,您正在使用以下内容导入库:

import * as my-library from 'my-library'


在本示例中,您使用的是subscribe和map函数。因此,您需要导入可观察对象并映射到此组件

import {Observable} from 'rxjs/Observable';
import 'Rxjs/add/operator/map';
你的构造器应该是

 constructor(public http: Http) {     
    this.http = http;
    }
boot.ts

import {bootstrap}    from 'angular2/platform/browser';
import {AppComponent} from './app.component';
import {HTTP_PROVIDERS} from 'angular2/http';

bootstrap(AppComponent,[HTTP_PROVIDERS]);
index.html

<script>
  System.config({
    transpiler: 'typescript',
    typescriptOptions: { emitDecoratorMetadata: true },
    packages: {
        'app': {defaultExtension: 'ts'},
        '../node_modules/rxjs': { defaultExtension: 'js' }
    },
      paths: {
          'rxjs/operator/*': '../node_modules/rxjs/add/operator/*.js',
          'rxjs/*': '../node_modules/rxjs/*.js'
      }
  });
  System.import('app/boot')
        .then(null, console.error.bind(console));
</script>

System.config({
transpiler:'typescript',
typescriptOptions:{emitDecoratorMetadata:true},
套餐:{
'app':{defaultExtension:'ts'},
“../node_modules/rxjs”:{defaultExtension:'js'}
},
路径:{
“rxjs/operator/*”:“../node_modules/rxjs/add/operator/*.js”,
“rxjs/*”:“../node_modules/rxjs/*.js”
}
});
System.import('app/boot')
.then(null,console.error.bind(console));

在本示例中,您使用的是订阅和映射功能。因此,您需要导入可观察对象并映射到此组件

import {Observable} from 'rxjs/Observable';
import 'Rxjs/add/operator/map';
你的构造器应该是

 constructor(public http: Http) {     
    this.http = http;
    }
boot.ts

import {bootstrap}    from 'angular2/platform/browser';
import {AppComponent} from './app.component';
import {HTTP_PROVIDERS} from 'angular2/http';

bootstrap(AppComponent,[HTTP_PROVIDERS]);
index.html

<script>
  System.config({
    transpiler: 'typescript',
    typescriptOptions: { emitDecoratorMetadata: true },
    packages: {
        'app': {defaultExtension: 'ts'},
        '../node_modules/rxjs': { defaultExtension: 'js' }
    },
      paths: {
          'rxjs/operator/*': '../node_modules/rxjs/add/operator/*.js',
          'rxjs/*': '../node_modules/rxjs/*.js'
      }
  });
  System.import('app/boot')
        .then(null, console.error.bind(console));
</script>

System.config({
transpiler:'typescript',
typescriptOptions:{emitDecoratorMetadata:true},
套餐:{
'app':{defaultExtension:'ts'},
“../node_modules/rxjs”:{defaultExtension:'js'}
},
路径:{
“rxjs/operator/*”:“../node_modules/rxjs/add/operator/*.js”,
“rxjs/*”:“../node_modules/rxjs/*.js”
}
});
System.import('app/boot')
.then(null,console.error.bind(console));

这只会导致问题,system.src正在查找不存在的Rxjs/add/operator/Map您需要在index.html文件中包含路径。这只会导致问题,system.src正在查找不存在的Rxjs/add/operator/Map您需要在index.html文件中包含路径。