保持不被接受(承诺):错误:模板解析错误:'component'不是已知元素: [英] Keep getting Uncaught (in promise): Error: Template parse errors: 'component' is not a known element:

查看:133
本文介绍了保持不被接受(承诺):错误:模板解析错误:'component'不是已知元素:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一些代码片段.

组件:

import { Component } from '@angular/core';

@Component({
 selector: 'app-generalstatistics',
 templateUrl: './generalstatistics.component.html',
 styleUrls: ['./generalstatistics.component.css']
})
export class Generalstatistics  {
 public data: Array<Object>;
 constructor() { }
}

app.module.ts:

app.module.ts:

import { Generalstatistics } from './views/generalstatistics/generalstatistics.component';

   declarations: [
   ....
   Generalstatistics
],

DashboardModule/DashboardComponent.html中的实现:

Implementation in DashboardModule/DashboardComponent.html:

<div class="chart-wrapper mt-3 mx-3" style="height:70px;">
      <app-generalstatistics></app-generalstatistics>
    </div>

错误:

core.js:4002 ERROR Error: Uncaught (in promise): Error: Template parse errors:
'app-generalstatistics' is not a known element:
1. If 'app-generalstatistics' is an Angular component, then verify that it is part of this module.
2. If 'app-generalstatistics' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the 
'@NgModule.schemas' of this component to suppress this message. ("
    </div>
    <div class="chart-wrapper mt-3 mx-3" style="height:70px;">
      [ERROR ->]<app-generalstatistics></app-generalstatistics>
    </div>
  </div>
 "): ng:///DashboardModule/DashboardComponent.html

我到底在做什么错?

感谢您的帮助.

推荐答案

我假设您应将 app-generalstatistics 组件添加到<以及code> DashboardModule (实际上就在您使用的地方)

I assume you should add the app-generalstatistics component to the declarations: [] of your DashboardModule as well (Actually just where you use it)

Afaik NgModule是模板的编译上下文.因此,我们必须在使用该模块的每个模块中声明一个组件.

Afaik the NgModule is the compilation context for the templates. So we have to declare a component in every module in which it is used.

为什么?在我的想法中:否则编译可能要花一些时间,因为在那种情况下,Angular必须去搜索组件定义,并且可能依赖于任何依赖关系,包括第三方.

Why? In my thinking: Otherwise the compilation would probably take a while because in that case Angular would have to go searching for the components definition and that could possibly be in any dependency including 3rd parties.

这篇关于保持不被接受(承诺):错误:模板解析错误:'component'不是已知元素:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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