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;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 汇编语言除法溢出问题
  • ¥50 C++实现删除N个数据列表共有的元素
  • ¥15 Visual Studio问题
  • ¥15 state显示变量是字符串形式,但是仍然红色,无法引用,并显示类型不匹配
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗