Angular Universal Render 等待 Http 结果 Observable Subscriber

标签 angular asp.net-web-api asp.net-core rxjs

我有一个调用 ASP.Net Web API 服务的 Angular Universal 应用程序。

我已经将控制台写入对 Web 服务的 Angular 调用(从组件的构造函数或 ngOnInit 启动,我都尝试过)和 Typescript 中的订阅者函数以及 Web API 方法。

运行应用程序时,我注意到 Angular Universal 应用程序不会等待 Web API 的结果。

我得到的最后一个控制台写入消息来自 Web API,而不是来自订阅者调用中的函数。

如何强制 Angular Universal 应用程序在完成渲染之前等待结果?

最佳答案

只要您使用的是 Angular HttpClient ,服务器端渲染应该等待它完成,然后再提供您的响应。
如果这对您不起作用,请尝试使用 | async 订阅调用模板代码中的管道。
例如:

export class MyComponent {
  public response$ = this.httpClient.get<string>('https://my.api');

  constructor(private httpClient: HttpClient){}
}
<span> {{ response$ | async }} </span>

关于Angular Universal Render 等待 Http 结果 Observable Subscriber,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43289317/

相关文章:

Angular 4,在没有实际路由的情况下获取路由数据

angular - 子组件中的 ExpressionChangedAfterItHasBeenCheckedError

.net - Web API:HttpResponseMessage中的内容

c# - IWebHost : Calling Run() vs RunAsync()

javascript - Angular 2组件@Input不工作

task-parallel-library - Web API中的长期任务

c# - 使用 odata 和 web api 2 时创建 cookie

asp.net - http 服务器的责任与使用此服务器托管的 Web 应用程序的责任

c# - ASP.NET Core web.config requestFiltering 不覆盖 applicationhost.config

sqlite - 使用 Ionic SqlStorage 时,sqlite 数据存储在哪里?