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

huangapple go评论129阅读模式
英文:

Can't run GolLang backend with dart and angular2 client

问题

我使用这个教程来制作一个使用GoLang、Angular2和Dart的Web应用程序,但是当我通过控制台命令"backend"启动后端,并在浏览器中路由到"localhost:8080/"时,它应该调用Dart类"Hello"的方法,但它没有调用,并且我收到404错误。我从教程中获取的所有代码,并没有做任何更改。我找不到其他的教程。你能解释一下我做错了什么吗?

GoLang代码:

  1. func main() {
  2. http.Handle("/", http.FileServer(http.Dir("./app/web/")))
  3. fmt.Println("Text")
  4. http.HandleFunc("/api/hello", helloWorld)
  5. http.ListenAndServe(":8080", nil)
  6. }
  7. func helloWorld(w http.ResponseWriter, r *http.Request) {
  8. data := struct {
  9. Message string
  10. }{
  11. "Hello, World",
  12. }
  13. if err := json.NewEncoder(w).Encode(data); err != nil {
  14. log.Println(err)
  15. }
  16. }

Angular Dart代码:

  1. class AppComponent {
  2. Hello hello = new Hello();
  3. }
  4. class Hello{
  5. String message;
  6. Hello(){
  7. HttpRequest.getString('/api/hello')
  8. .then((String content) {
  9. Map parsedMap = JSON.decode(content);
  10. message = parsedMap["Message"];
  11. })
  12. .catchError((Error error) {
  13. print(error.toString());
  14. });
  15. }
  16. }

项目结构:

无法使用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:

  1. func main() {
  2. http.Handle("/", http.FileServer(http.Dir("./app/web/")))
  3. fmt.Println("Text")
  4. http.HandleFunc("/api/hello", helloWorld)
  5. http.ListenAndServe(":8080", nil)
  6. }
  7. func helloWorld(w http.ResponseWriter, r *http.Request) {
  8. data := struct {
  9. Message string
  10. }{
  11. "Hello, World",
  12. }
  13. if err := json.NewEncoder(w).Encode(data); err != nil {
  14. log.Println(err)
  15. }
  16. }

and angular dart code:

  1. class AppComponent {
  2. Hello hello = new Hello();
  3. }
  4. class Hello{
  5. String message;
  6. Hello(){
  7. HttpRequest.getString('/api/hello')
  8. .then((String content) {
  9. Map parsedMap = JSON.decode(content);
  10. message = parsedMap["Message"];
  11. })
  12. .catchError((Error error) {
  13. print(error.toString());
  14. });
  15. }
  16. }

and project structure :无法使用Dart和Angular2客户端运行GolLang后端。

答案1

得分: 1

教程已经相当旧了。你需要切换到HashLocationStrategy(据我所知,那是当时的默认选项)。

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

  1. bootstrap(AppComponent);

改为

  1. bootstrap(AppComponent, [
  2. ROUTER_PROVIDERS,
  3. provide(LocationStrategy, {useClass: HashLocationStrategy})
  4. ]);

你还需要添加一些额外的导入

  1. import 'package:angular2/router.dart'
  2. show
  3. HashLocationStrategy,
  4. LocationStrategy,
  5. ROUTER_PROVIDERS;
英文:

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

  1. bootstrap(AppComponent);

to

  1. bootstrap(AppComponent, [
  2. ROUTER_PROVIDERS,
  3. provide(LocationStrategy, {useClass: HashLocationStrategy})
  4. ]);

You also need to add some additional imports

  1. import 'package:angular2/router.dart'
  2. show
  3. HashLocationStrategy,
  4. LocationStrategy,
  5. ROUTER_PROVIDERS;

huangapple
  • 本文由 发表于 2016年3月8日 21:17:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/35868480.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定