douyiavxxh02727 2016-03-08 13:17
浏览 68
已采纳

无法使用Dart和angular2客户端运行GolLang后端

I use this tutorial to make a web application with GoLang, Angular2 and Dart, but when i start backend by console command 'backend', and route in browser to "localhost:8080/" it must call method from Dart's class "Hello" but it doesn't call, and i get 404 error. All code i got from tutorial and didn't change anything. And i can't find any other tutorials. Can you explain me what wrong am i doing?

GoLang code:

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)
   }
}

and angular dart code:

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());
        });
  }
}

and project structure :enter image description here

  • 写回答

1条回答 默认 最新

  • dp198879 2016-03-08 13:31
    关注

    The tutorial is quite old. You need to switch to HashLocationStrategy (which was the default back then as far as I know).

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

    Change

    bootstrap(AppComponent);
    

    to

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

    You also need to add some additional imports

    import 'package:angular2/router.dart'
        show
            HashLocationStrategy,
            LocationStrategy,
            ROUTER_PROVIDERS;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 模电中二极管,三极管和电容的应用
  • ¥15 关于模型导入UNITY的.FBX: Check external application preferences.警告。
  • ¥15 气象网格数据与卫星轨道数据如何匹配
  • ¥100 java ee ssm项目 悬赏,感兴趣直接联系我
  • ¥15 微软账户问题不小心注销了好像
  • ¥15 x264库中预测模式字IPM、运动向量差MVD、量化后的DCT系数的位置
  • ¥15 curl 命令调用正常,程序调用报 java.net.ConnectException: connection refused
  • ¥20 关于web前端如何播放二次加密m3u8视频的问题
  • ¥15 使用百度地图api 位置函数报错?
  • ¥15 metamask如何添加TRON自定义网络