【问题标题】:Can't display multiple components in a Singular Page Application (Angular)无法在单一页面应用程序 (Angular) 中显示多个组件
【发布时间】:2019-05-16 18:35:22
【问题描述】:

我正在尝试开发一个基本网站,我必须在其中显示从 MQTT 主题接收到的数据(仪表板)。我无法显示第二个组件,它是一个应该显示从 MQTT 代理接收到的数据的仪表。我不知道如何将此图形插件连接到代理。

我已经尝试通过开发一个仅显示仪表元素的新独立应用程序来解决问题。 我发现了不同类型的仪表库。

App.component.html:

 Speed: {{value}}
  app-gauge /app-gauge

Gauge.component.ts

export class GaugeComponent implements OnInit {
  public value$: Observable<number>;
  ngOnInit() {
    this.value$ = interval(1000).pipe(
      map(() => Math.random() * 120)
    );

仪表应该出现并开始生成/显示随机数(链接到我遵循的指南:http://www.wayneparrott.com/add-a-realtime-gauge-to-your-angular-project/

【问题讨论】:

  • app.component.html的html和你的问题一样吗?
  • 是的。我试图将“”放在 index.html 中,但没有结果。如果我将第二个组件“仪表”放在 app.component.html (主要的)中,它只会显示(但不会启动)。
  • 没有尖括号,您的 html 无效。你检查过你的控制台有没有错误?

标签: html css angular typescript mqtt


【解决方案1】:

根据您的 cmets,您的组件似乎正在显示,但您的 html 中的 value 没有改变。这样做的原因是因为您使用Observable 在每 1 秒后发出随机值,因此为了使其正常工作,您需要订阅 Observable,在本例中为 value$。您可以使用async 管道订阅它。

您还需要更改一些代码。您在gauge.component.ts 中定义了您的Observable,但您在app.component.html 中访问它

将您的代码从 guage.component.ts 移动到 app.component.ts

您的app.component.ts 应如下所示:

export class AppComponent implements OnInit {
  public value$: Observable<number>;
  ngOnInit() {
    this.value$ = interval(1000).pipe(
      map(() => Math.random() * 120)
    );

现在将您的 app.component.html 更改为以下内容:

Speed: {{ value$ | async }}
  <app-gauge> </app-gauge>

【讨论】:

  • 未捕获错误:模板解析错误:'radial-gauge' 不是已知元素: 1. 如果'radial-gauge' 是 Angular 组件,则验证它是否是该模块的一部分。 2. 如果“radial-gauge”是一个 Web 组件,则将“CUSTOM_ELEMENTS_SCHEMA”添加到该组件的“@NgModule.schemas”以禁止显示此消息。
  • 这是chrome的控制台返回给我的错误
  • 请查看我使用的代码app-guage 而不是radial-gauge
  • 您在 html 中使用 radial-gauge,这就是您收到此错误的原因
  • 现在给我这个:
猜你喜欢
  • 1970-01-01
  • 2020-08-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-27
  • 2016-02-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多