2016-07-18 10 views
1

Ich verwende Angular2 Beta 17 (letzte Version) mit ASP.NET Kern 1 MVC-Anwendung, ich versuche http wie folgt zu verwenden:Angular2 Http-Modul erzeugt 404 nicht Fehler gefunden

import {Http} from 'Angular2/http'; 
import "rxjs/add/operator/map" 
import { Injectable } from 'angular2/core'; 
@Injectable() 
export class PostsService 
{ 
    constructor(private _http: Http) { 
    } 
    getPosts() 
    { 
     return this._http.get("http://jsonplaceholder.typicode.com/posts") 
      .map(res => res.json()); 
    }} 

Nach mit dem Service innerhalb der Komponente habe ich einen Fehler in Chrom: Fehler beim Laden der Ressource: Der Server antwortete mit einem Status von 404 (nicht gefunden). chrome error details.

Komponente den Dienst wie die folgende Aufruf:

import {Component} from 'angular2/core'; 
import {PostsService} from '../services/posts.service' 
import {HTTP_PROVIDERS} from 'angular2/http'; 


@Component({ 
    selector: 'post-list', 
    template: 
    ` 


`, 
    providers: [PostsService, HTTP_PROVIDERS] 

}) 
export class PostsComponent 
{ 
    constructor(private _postsService: PostsService) 
    { 
     this._postsService.getPosts().subscribe(posts => console.log(posts)); 
    } 
} 

Beachten Sie, dass alle in index.html wie folgt importiert js:

<section id="content"> 
    <div class="container"> 
     <div class="row"> 
      <my-app> 
       <div class="alert alert-info" role="alert"> 
        <h3>Loading...</h3> 
       </div> 
      </my-app> 
     </div> 
    </div> 
</section> 

@section Scripts { 
    <script> 
     System.config({ 
      packages: { 
       'app': { defaultExtension: 'js' }, 
       'lib': { defaultExtension: 'js' }, 
      }, 
     }); 

     System.import('app/boot') 
      .then(null, console.error.bind(console)); 
    </script> 


} 
+0

Könnte http://stackoverflow.com/questions/31415052/angular-2-0-router sein -nicht-working-on-reloading-the-browser –

+0

Das sieht nach einer defekten SystemJS-Konfiguration aus und 'Angular2/http' ist wahrscheinlich auch nicht der richtige Import (Angular hat ein Kleinbuchstaben-Modul) – rinukkusu

Antwort

1

Mit Angular2 Beta-Versionen, müssen Sie umfassen Die http.js Datei in Ihrer Haupt-HTML-Datei:

<script src="node_modules/angular2/bundles/angular2-polyfills.js"></script> 
<script src="node_modules/systemjs/dist/system.src.js"></script> 
<script src="node_modules/rxjs/bundles/Rx.js"></script> 
<script src="node_modules/angular2/bundles/angular2.dev.js"></script> 
<script src="node_modules/angular2/bundles/http.dev.js"></script> <----- 

Mit RC-Versionen haben sich die Dinge seit dem geändert, jetzt müssen Sie Angular2-Module (einschließlich der HTTP-Version) mit Ihrer SystemJS-Konfiguration konfigurieren