Angular 通用 + 外部 API [英] Angular Universal + External API

查看:25
本文介绍了Angular 通用 + 外部 API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道 Angular Universal 仍在开发中.因此,我考虑将 Angular Universal 用于定期从外部 API 服务器(大约每 10 秒通过 Http 服务)提取数据的 Web 应用程序.这些数据随后会显示在网络应用上.

I know that Angular Universal is still in development. So I have thought about using Angular Universal for a web app that regularly pulls data from an external API server (about every 10 seconds via the Http service). This data is then displayed on the web app.

我遇到了几个尚未找到简单解决方案的问题:

I have come across several problems that I haven't found an easy solution for yet:

  1. Angular Universal 显然需要来自 API 服务器的数据来预渲染页面.我不想每次请求 Web 应用程序 HTML 时都从 nodeJS 查询 API 服务器.如果 nodeJS 服务器的行为"与一个客户端 Web 应用程序实例完全一样,并且每 10 秒查询一次 API 服务器,以 10 秒的间隔缓存"这些数据并使用它来呈现 HTML,那就足够了.
  2. 客户端 Web 应用程序将来自 API 服务器的数据存储在某个私有成员变量中.然后使用该成员变量通过 ngFor、bindings 等构建 HTML.但是这个成员变量在开始时是空的/未定义的.我不希望客户端 Web 应用在启动时擦除预渲染的 HTML,因为这会破坏 Angular Universal 的一些想法(不是 SEO,但会减少加载时间).

我希望你明白我在这里想要实现的目标.您是否知道如何解决这个问题,或者甚至有一种官方"的方法来做到这一点?我敢肯定,我不是唯一一个尝试做类似事情的人.

I hope you understand what I'm trying to achieve here. Do you have an idea how to approach this, or is there even an "official" way to do that? I am sure I am not the only one trying to do something like that.

推荐答案

Angular Universal 有两个常量 isNode &是浏览器

Angular Universal have two constants isNode & IsBrowser

if(isNode){

   // it's Node Server

} else {
  //it's Browser
}

这篇关于Angular 通用 + 外部 API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆