首页 > 解决方案 > 发布 - 角 5

问题描述

我对 Angular 还很陌生,并且一直无法从后端的 SpringREST 获取数据。所以场景是:我JSON将从后端获取一个字符串POST(JSON 数据将作为 POST 重定向到我的站点托管链接),我需要捕获该 JSON 字符串并将其显示在 UI 上。我不确定postMethodin dataservice.ts 是否应该存在。

我在 stackoverflow 上搜索并想出了以下代码,这在我的场景中似乎不起作用:

组件.ts

import { MyDataService } from './services/my-data.service';
constructor(private posting: MyDataService
  ) {}

  ngOnInit() {
    this.posting.postMethod().subscribe(
    (response => { 
      console.log(response)
    }));
}
}

数据服务.ts

@Injectable()

export class MyDataService {

  constructor(private http: Http)
  { }


 postMethod(model: any  ) {
    return this.http.post("http ://", model)
      .map(res => res.json());
  }
}

标签: javascriptangulartypescript

解决方案


正如错误所说,您需要在调用时将参数传递给服务

 this.posting.postMethod(model).subscribe(
    (response => { 
      console.log(response)
  }));

推荐阅读