2016-11-23 11 views
1

私はhttps://angular.io/docs/ts/latest/tutorial/toh-pt6.htmlからコードを適用し、ページの英雄の詳細を除いて良い実行します。http://localhost:3000/detail/12Angular2チュー​​トリアル(英雄のツアー):EXCEPTION:ステータス応答:404 URLが見つかりません:ヌル

error

ファイルアプリ。 module.ts

import './rxjs-extensions'; 
import { NgModule }  from '@angular/core'; 
import { BrowserModule } from '@angular/platform-browser'; 
import { FormsModule } from '@angular/forms'; 
import { HttpModule } from '@angular/http'; 
import { APP_BASE_HREF } from '@angular/common'; 

import { AppRoutingModule } from './app-routing.module'; 

// Imports for loading & configuring the in-memory web api 
import { InMemoryWebApiModule } from 'angular-in-memory-web-api'; 
import { InMemoryDataService } from './in-memory-data.service'; 

import { AppComponent }   from './app.component'; 
import { DashboardComponent } from './dashboard.component'; 
import { HeroesComponent }  from './heroes.component'; 
import { HeroDetailComponent } from './hero-detail.component'; 
import { HeroService }   from './hero.service'; 
import { HeroSearchComponent } from './hero-search.component'; 

@NgModule({ 
    imports: [ 
    BrowserModule, 
    FormsModule, 
    HttpModule, 
    InMemoryWebApiModule.forRoot(InMemoryDataService), 
    AppRoutingModule 
    ], 
    declarations: [ 
    AppComponent, 
    DashboardComponent, 
    HeroDetailComponent, 
    HeroesComponent, 
    HeroSearchComponent 
    ], 
    providers: [ {provide: APP_BASE_HREF, useValue : '/' }, HeroService ], 
    bootstrap: [ AppComponent ] 
}) 
export class AppModule { } 

ファイルhero.service.ts

import { Injectable } from '@angular/core'; 
import { Headers, Http } from '@angular/http'; 

import 'rxjs/add/operator/toPromise'; 

import { Hero } from './hero'; 

@Injectable() 
export class HeroService { 

    private headers = new Headers({'Content-Type': 'application/json'}); 
    private heroesUrl = 'app/heroes'; // URL to web api 

    constructor(private http: Http) { } 

    getHeroes(): Promise<Hero[]> { 
    return this.http.get(this.heroesUrl) 
       .toPromise() 
       .then(response => response.json().data as Hero[]) 
       .catch(this.handleError); 
    } 

    getHero(id: number): Promise<Hero> { 
    return this.getHeroes() 
       .then(heroes => heroes.find(hero => hero.id === id)); 
    } 

    delete(id: number): Promise<void> { 
    const url = `${this.heroesUrl}/${id}`; 
    return this.http.delete(url, {headers: this.headers}) 
     .toPromise() 
     .then(() => null) 
     .catch(this.handleError); 
    } 

    create(name: string): Promise<Hero> { 
    return this.http 
     .post(this.heroesUrl, JSON.stringify({name: name}), {headers: this.headers}) 
     .toPromise() 
     .then(res => res.json().data) 
     .catch(this.handleError); 
    } 

    update(hero: Hero): Promise<Hero> { 
    const url = `${this.heroesUrl}/${hero.id}`; 
    return this.http 
     .put(url, JSON.stringify(hero), {headers: this.headers}) 
     .toPromise() 
     .then(() => hero) 
     .catch(this.handleError); 
    } 

    private handleError(error: any): Promise<any> { 
    console.error('An error occurred', error); // for demo purposes only 
    return Promise.reject(error.message || error); 
    } 
} 

index.htmlファイル

<!DOCTYPE html> 
<html> 
    <head> 
    <title>Angular QuickStart</title> 
    <meta charset="UTF-8"> 
    <meta name="viewport" content="width=device-width, initial-scale=1"> 
    <link rel="stylesheet" href="styles.css"> 

    <!-- Polyfill(s) for older browsers --> 
    <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> 
    </head> 

    <body> 
    <my-app>Loading...</my-app> 
    </body> 
</html> 

角度2チュートリアルの最初のデモ(英雄のツアー)で私を助けてください。ありがとう!

+0

private heroesUrl = 'app/heroes'; // URL to web api

を変更する必要がありますは、httpようにそれを試してみました:// localhostを:3000 /ディテール/:ID? ':id'は英雄のidに置き換えなければなりません。 –

+0

私はhttp:// localhost:3000/detail/12を試してみましたが、実行しません:( – lhpkhanh

+0

''/heroes'のルーティングリンクを作ったのですか? –

答えて

0

私はわからないが、それは完全なURLではないようです:あなたは完全なURLを取得している場合

private heroesUrl = 'app/heroes'; // URL to web api 

をチェックしてください。

+0

I試してみるが、走らない:( – lhpkhanh

0

tutorialによると、あなたは

private heroesUrl = 'api/heroes'; // URL to web api

関連する問題