2017-01-23 176 views
0

這是我的index.htmlAngular2得到錯誤404未找到

<!DOCTYPE html> 
<html> 
    <head> 
    <meta charset="utf-8"> 
    <meta name="viewport" content="width=device-width"> 
    <title>Learn AngularJS2 </title> 
    <meta charset="utf-8"> 
    <!-- bootstrap --> 
    <meta name="viewport" content="width=device-width, initial-scale=1"> 
    <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> 
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> 
    <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> 
    <!-- fonts --> 
    <link href='https://fonts.googleapis.com/css?family=Lato:700,400,100,200,300' rel='stylesheet' type='text/css'> 
    <!-- css link --> 
    <link rel="stylesheet" href="css/style.css"> 
    <!-- Source files --> 
    <script src="js/lib/angular2/es6-shim.min.js"></script> 
    <script src="js/lib/angular2/system-polyfills.js"></script> 

    <script src="js/lib/angular2/angular2-polyfills.js"></script> 
    <script src="js/lib/angular2/system.src.js"></script> 
    <script src="js/lib/angular2/Rx.js"></script> 
    <script src="js/lib/angular2/angular2.dev.js"></script> 

    <script src='https://code.angularjs.org/2.0.0-beta.0/http.dev.js'></script> 


    <!-- config --> 

    <script> 
     System.config({ 
     packages: { 
      js: { 
      format: 'register', 
      defaultExtension: 'js' 
      } 
     } 
     }); 
     System.import('js/boot') 
     .then(null, console.error.bind(console)); 
    </script> 
    </head> 
    <!-- Body --> 
<body> 
<div class="container"> 

    <httptest>Loading...</httptest> 

</div> 
</body> 
</html> 

,這是HTTP組分

import {Component} from 'angular2/core'; 
import {httpService} from './http_service'; 

@Component({ 
    selector: 'httptest', 
    template:` 
    <button (click)="onGet()">getData</button> 
    <p>Output : {{getData}}</p> 
    `, 
    providers:[httpService] 
}) 


export class httpComp{ 

    getData:string; 
    constructor(
    private httpService:httpService){} 
    onGet(){ 
    this.httpService.getData() 
    .subscribe(
     data => this.getData=JSON.stringfy(data), 
     error => alert(error), 
     () => console.log("finish") 
    ); 
    } 

} ,這是我的HTTP服務

import {Injectable} from '@angular2/core'; 
import {Http} from 'angular2/http'; 
import 'rxjs/add/operator/map'; 


@Injectable() 

export class httpService{ 
constructor (private _http:Http){ 
    getData(){ 
     return 
this._http.get('http://localhost:3000/blahblah') 
     .map(res => res.json()) 
     } 

    } 

    } 

即boot.ts

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

import {httpComp} from './http.component'; 

bootstrap(httpComp); 

當我運行我得到這些錯誤

system.src.js:1049 GET http://localhost:8000/@angular2/core 404(未找到) 和此錯誤

angular2-polyfills.js:138錯誤:XHR錯誤(404未找到)裝載http://localhost:8000/@angular2/core(...)

+0

您正在使用哪個angular2的版本? – echonax

+0

「angular2」:「2.0.0-beta.2」, – flower

+0

http://imgur.com/QymsbpI – flower

回答

2

@angular/core符號在betaalpha進口就像angular2/core帶着2.0.0.rc.0版本。你混淆了你的進口。

來源:https://github.com/angular/angular/blob/master/CHANGELOG.md#200-rc0-2016-05-02

import {Component} from 'angular2/core'; 
import {Http, HTTP_PROVIDERS} from 'angular2/http'; 
import {httpService} from './http_service'; 


@Component({ 
    selector: 'httptest', 
    template:` 
    <button (click)="onGet()">getData</button> 
    <p>Output : {{getData}}</p> 
    `, 
    providers:[Http, HTTP_PROVIDERS, httpService] 
}) 


export class httpComp{ 

    getData:string; 
    constructor(
    private httpService:httpService){} 
    onGet(){ 
    this.httpService.getData() 
    .subscribe(
     data => this.getData=JSON.stringfy(data), 
     error => alert(error), 
     () => console.log("finish") 
    ); 
    } 
+0

現在的工作,並呈現數據,但我有一個簡單的問題,它呈現數據像這樣http:// imgur淺色。 com/qUVH93z – flower

+0

@flower你的問題不包括顏色問題的必要信息:/ – echonax

+0

那麼如何爲它做設計? – flower