2016-03-08 153 views
0

我使用此tutorial來使用GoLang,Angular2和Dart創建Web應用程序,但是當我通過控制檯命令「backend」啓動後端並在瀏覽器中路由到「localhost: 8080 /「它必須從Dart的類」Hello「調用方法,但它不會調用,並且我得到404錯誤。我從教程中獲得的所有代碼並沒有改變任何內容。我找不到任何其他教程。你能解釋我有什麼問題嗎?無法使用dart和angular2客戶端運行GolLang後端

GoLang代碼:

func main() { 
    http.Handle("/", http.FileServer(http.Dir("./app/web/"))) 

    fmt.Println("Text") 
    http.HandleFunc("/api/hello", helloWorld) 
    http.ListenAndServe(":8080", nil) 

} 
func helloWorld(w http.ResponseWriter, r *http.Request) { 
    data := struct { 
     Message string 
    }{ 
     "Hello, World", 
    } 

    if err := json.NewEncoder(w).Encode(data); err != nil { 
     log.Println(err) 
    } 
} 

和角鏢代碼:

class AppComponent { 
    Hello hello = new Hello(); 
} 

class Hello{ 
    String message; 

    Hello(){ 

    HttpRequest.getString('/api/hello') 
     .then((String content) { 
      Map parsedMap = JSON.decode(content); 
      message = parsedMap["Message"]; 
     }) 
     .catchError((Error error) { 
      print(error.toString()); 
     }); 
    } 
} 

和項目結構:enter image description here

+4

什麼是 「應用程序」,「應用程序沒有按」連續等到飛鏢一側「?爲什麼「應用程序」會連接到「Dart side」?如果在客戶端(使用Angular)上使用Dart,則客戶端必須連接到服務器而不是其他方式。 –

+0

@GünterZöchbauer當我進入瀏覽器本地主機:8080 /它必須調用「Hello」處理程序,它描述在web/main.dart但它不這樣做,只是重定向到404 – EgorkZe

+0

@GünterZöchbauer是正確的。 嘗試本地化問題。首先檢查 - 服務器端是否正在響應HTTP請求(從bwoser輸入URL到地址欄,而不是來自Dart)。如果它起作用,則需要提供有關如何調用服務器的信息以及錯誤日誌。 http:// localhost:8080/api/hello是否有效? –

回答

1

本教程是很老。您需要切換到HashLocationStrategy(據我所知,這是默認值)。

https://angular.io/docs/ts/latest/api/router/HashLocationStrategy-class.html

變化

bootstrap(AppComponent); 

bootstrap(AppComponent, [ 
    ROUTER_PROVIDERS, 
    provide(LocationStrategy, {useClass: HashLocationStrategy}) 
]); 

您還需要在添加一些額外的進口

import 'package:angular2/router.dart' 
    show 
     HashLocationStrategy, 
     LocationStrategy, 
     ROUTER_PROVIDERS; 
+0

我添加了你說的,但它沒有幫助(是否有新的教程飛鏢angular2和去? – EgorkZe

+1

我還沒有看到Angular2和Go的任何教程。 –