CUSTOM_ELEMENTS_SCHEMA添加到NgModule.schemas仍然显示错误

Rap*_*ppe 119 karma-jasmine angular

我刚从Angular 2 rc4升级到rc6并且遇到了麻烦.

我在控制台上看到以下错误:

Unhandled Promise rejection: Template parse errors:
'cl-header' is not a known element:
1. If 'cl-header' is an Angular component, then verify that it is part of this module.
2. If 'cl-header' is a Web Component then add "CUSTOM_ELEMENTS_SCHEMA" to the '@NgModule.schema' of this component to suppress this message. ("<main>
    [ERROR ->]<cl-header>Loading Header...</cl-header>
    <div class="container-fluid">
      <cl-feedbackcontai"): AppComponent@1:4
Run Code Online (Sandbox Code Playgroud)

这是我的标题组件:

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

// own service
import { AuthenticationService } from '../../../services/authentication/authentication.service.ts';

import '../../../../../public/css/styles.css';

@Component({
  selector: 'cl-header',
  templateUrl: './header.component.html',
  styleUrls: ['./header.component.css']
})
export class HeaderComponent { // more code here... }
Run Code Online (Sandbox Code Playgroud)

这是我的标题模块:

import { NgModule, CUSTOM_ELEMENTS_SCHEMA }      from '@angular/core';
import { RouterModule } from '@angular/router';
import { CommonModule }      from '@angular/common';
import { FormsModule }      from '@angular/forms';

import { HeaderComponent }  from './../../../components/util_components/header/header.component.ts';

@NgModule({
    declarations: [ HeaderComponent ],
    bootstrap:    [ HeaderComponent ],
    imports: [ RouterModule, CommonModule, FormsModule ],
    schemas: [ CUSTOM_ELEMENTS_SCHEMA ]
})
export class HeaderModule { }
Run Code Online (Sandbox Code Playgroud)

我创建了一个名为util module的包装器模块,它导入了HeaderModule:

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

import {HeaderModule} from "./header/header.module";
// ...

@NgModule({
    declarations: [ ],
    bootstrap:    [ ],
    imports: [ HeaderModule]
})
export class UtilModule { }
Run Code Online (Sandbox Code Playgroud)

最终由AppModule导入:

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

import { BrowserModule } from '@angular/platform-browser';

import { AppComponent }  from './app.component';

import {UtilModule} from "./modules/util_modules/util.module";
import {RoutingModule} from "./modules/routing_modules/routing.module";

@NgModule({
    bootstrap: [AppComponent],
    declarations: [AppComponent],
    imports: [BrowserModule, UtilModule, RoutingModule]
})
export class AppModule { }
Run Code Online (Sandbox Code Playgroud)

根据我的理解,我遵循错误消息的指示使用SCHEMA来克服错误.但它似乎不起作用.我究竟做错了什么?(我希望我现在看不到任何明显的东西.过去6个小时都在升级到这个版本......)

Cal*_*leb 89

只是想补充一点.

使用新的角度2.0.0最终版本(2016年9月14日),如果您使用自定义html标签,那么它将报告Template parse errors.自定义标记是您在HTML中使用的标记,不是这些标记之一.

看起来schemas: [ CUSTOM_ELEMENTS_SCHEMA ]需要将行添加到使用自定义HTML标记的每个组件中.

编辑:schemas声明只需要在一个@NgModule装饰.下面的示例显示了一个带有自定义组件的自定义模块,该组件CustomComponent允许html模板中的任何html标记用于该组件.

custom.module.ts

import { NgModule, CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
import { CommonModule } from '@angular/common';

import { CustomComponent } from './custom.component';

@NgModule({
  declarations: [ CustomComponent ],
  exports: [ CustomComponent ],
  imports: [ CommonModule ],
  schemas: [ CUSTOM_ELEMENTS_SCHEMA ]
})
export class CustomModule {}
Run Code Online (Sandbox Code Playgroud)

custom.component.ts

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

@Component({
  selector: 'my-custom-component',
  templateUrl: 'custom.component.html'
})
export class CustomComponent implements OnInit {
  constructor () {}
  ngOnInit () {}
}
Run Code Online (Sandbox Code Playgroud)

custom.component.html

在这里,您可以使用任何您想要的HTML标记.

<div class="container">
  <boogey-man></boogey-man>
  <my-minion class="one-eyed">
    <job class="plumber"></job>
  </my-minion>
</div>
Run Code Online (Sandbox Code Playgroud)

  • 谢谢Caleb.我在运行一个简单的测试时遇到了错误.我想通了......我没有将`CUSTOM_ELEMENTS_SCHEMA`添加到我的单元测试假模块中.我一做到这一点就停止了抱怨. (7认同)
  • 有没有办法定义允许的自定义元素?使用 `CUSTOM_ELEMENTS_SCHEMA` 可能会导致难以发现的错误,但我想在我的控件中为 `ng-content` 部分使用自定义元素名称,而不是那些导致错误的特定元素名称,也不会为它们创建组件ng-内容... (2认同)
  • 嘿@DannyBullis,而不是`Component`装饰器,可以在`NgModule`装饰器中找到。您需要为该组件创建一个模块,然后可以在其中指定架构。[链接到文档和示例。](https://angular.io/docs/ts/latest/api/core/index/NgModule-interface.html) (2认同)

Rap*_*ppe 77

嘿这是固定的做法

a)添加schemas: [ CUSTOM_ELEMENTS_SCHEMA ]到每个组件或

b)添加

import { NgModule, CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
Run Code Online (Sandbox Code Playgroud)

schemas: [
    CUSTOM_ELEMENTS_SCHEMA
],
Run Code Online (Sandbox Code Playgroud)

到你的模块.

干杯,拉斐尔

  • 不要忘记宣布它...它位于@ angular/core.这样的东西应该适合:'import {NgModule,CUSTOM_ELEMENTS_SCHEMA}来自'@ angular/core';` (6认同)
  • 角度 9 中不存在模式 (3认同)
  • 不; 没有解决我的问题...也许是 Angular 11 的问题 (2认同)

Dan*_*ert 26

如果您正在使用自定义元素测试组件,则在运行单元测试时也会出现这种情况.在这种情况下,需要将custom_elements_schema添加到在该组件的.spec.ts文件开头设置的testingModule中.以下是header.component.spec.ts设置如何开始的示例:

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

describe('HeaderComponent', () => {
  let component: HeaderComponent;
  let fixture: ComponentFixture<HeaderComponent>;

  beforeEach(async(() => {
    TestBed.configureTestingModule({
      declarations: [PrizeAddComponent],
      schemas: [
        CUSTOM_ELEMENTS_SCHEMA
      ],
    })
      .compileComponents();
  }));
Run Code Online (Sandbox Code Playgroud)

  • 谢谢!我花了 #&amp;@% 很长时间才找到这个。 (2认同)

Neg*_*gin 15

它对我有用:

在"App.module.ts"中:

1-

import CUSTOM_ELEMENTS_SCHEMA from `@angular/core`;
Run Code Online (Sandbox Code Playgroud)

2 - 添加

schemas: [
    CUSTOM_ELEMENTS_SCHEMA
]
Run Code Online (Sandbox Code Playgroud)

到了 @NgModule({})

-------------------------------------------------- -------> <----------------------------------------- --------------------

"app.module.ts"将如下所示:

import { NgModule, CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';

@NgModule({
  declarations: [],
  imports: [],
  schemas: [ CUSTOM_ELEMENTS_SCHEMA],
  providers: [],
  bootstrap: [AppComponent]
})

export class AppModule { }
Run Code Online (Sandbox Code Playgroud)

导出类AppModule {}

  • 但现在你的整个应用程序允许自定义标签. (2认同)

Mar*_*mer 10

它对我也没有用.我变了

CUSTOM_ELEMENTS_SCHEMA
Run Code Online (Sandbox Code Playgroud)

对于

NO_ERRORS_SCHEMA
Run Code Online (Sandbox Code Playgroud)

本文建议:https: //scotch.io/tutorials/angular-2-transclusion-using-ng-content

现在它有效.


Aru*_*run 9

util.component.ts

@Component({
    selector: 'app-logout',
    template: `<button class="btn btn-primary"(click)="logout()">Logout</button>`
})
export class LogoutComponent{}
Run Code Online (Sandbox Code Playgroud)

util.module.ts

@NgModule({
    imports: [...],
    exports: [
        LogoutComponent
    ],
    declarations: [LogoutComponent]
})
export class AccountModule{};
Run Code Online (Sandbox Code Playgroud)

LogoutComponent需要导出

dashboard.module.ts
导入AccountModule模块中我们要使用<app-logout> 'util.module'中的import {AccountModule};

@NgModule({
  imports: [
    CommonModule, AccountModule
  ],
  declarations: [DashboardComponent]
})
export class DashboardModule { }
Run Code Online (Sandbox Code Playgroud)

dashboard.component.ts

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

@Component({
  selector: 'app-dashboard',
  template: `<div><app-logout></app-logout></div>`
})
export class DashboardComponent implements OnInit {
  constructor() {}
  ngOnInit() {}
}
Run Code Online (Sandbox Code Playgroud)

我不需要导入和使用CUSTOM_ELEMENTS_SCHEMA.
但是当dashboard.module延迟加载时它无法正常工作.
CUSTOM_ELEMENTS_SCHEMA延迟加载的情况下使用时,会抑制错误,但组件不会添加到dom中.

  • 非常感谢,一周后,我发现它无法在 ionic 中使用延迟加载 (2认同)

T. *_*ord 9

对于包含 Angular Material 的组件,我的单元测试出现了类似的错误。根据上面@Dan Stirling-Talbert 的回答,将其添加到我的组件 .spec 文件中,并从我的单元测试中清除了错误。

Import { CUSTOM_ELEMENTS_SCHEMA } from '@angular/core'
Run Code Online (Sandbox Code Playgroud)

然后在生成的 beforeEach() 语句中添加模式:

beforeEach(asyn(() => {
    declarations: [ AboutComponent ],
    schemas: [ CUSTOM_ELEMENTS_SCHEMA ]
})
.compileComponents();
}));
Run Code Online (Sandbox Code Playgroud)

我的 Karma 错误是:如果“mat-panel-title”是一个 Web 组件,则将“CUSTOM_ELEMENTS_SCHEMA”添加到该组件的“@NgModule.schemas”以抑制此消息。


小智 5

只需阅读这篇文章并根据 Angular 2 文档:

export CUSTOM_ELEMENTS_SCHEMA
Defines a schema that will allow:

any non-Angular elements with a - in their name,
any properties on elements with a - in their name which is the common rule for custom elements.
Run Code Online (Sandbox Code Playgroud)

因此,为了以防万一有人遇到这个问题,一旦您将 CUSTOM_ELEMENTS_SCHEMA 添加到您的 NgModule 中,请确保您使用的任何新自定义元素的名称中都有“破折号”,例如。或等等

  • 名字里加个破折号?为什么要强加这样一个愚蠢的约定呢? (2认同)

Com*_*ine 5

这是一篇相当长的文章,它对这个问题给出了更详细的解释。

当您有多槽内容投影时,问题(就我而言)就出现了

另请参阅内容投影了解更多信息。

例如,如果您有一个如下所示的组件:

html 文件:

 <div>
  <span>
    <ng-content select="my-component-header">
      <!-- optional header slot here -->
    </ng-content>
  </span>

  <div class="my-component-content">
    <ng-content>
      <!-- content slot here -->
    </ng-content>
  </div>
</div>
Run Code Online (Sandbox Code Playgroud)

.ts 文件:

@Component({
  selector: 'my-component',
  templateUrl: './my-component.component.html',
  styleUrls: ['./my-component.component.scss'],
})
export class MyComponent {    
}
Run Code Online (Sandbox Code Playgroud)

你想像这样使用它:

<my-component>
  <my-component-header>
    <!-- this is optional --> 
    <p>html content here</p>
  </my-component-header>


  <p>blabla content</p>
  <!-- other html -->
</my-component>
Run Code Online (Sandbox Code Playgroud)

然后你会得到模板解析错误,该错误不是已知的 Angular 组件,事实上它不是 - 它只是对组件中 ng-content 的引用:

然后最简单的修复是添加

schemas: [
    CUSTOM_ELEMENTS_SCHEMA
],
Run Code Online (Sandbox Code Playgroud)

...到你的app.module.ts


但是有一个简单而干净的方法来解决这个问题 - 而不是使用<my-component-header>在插槽中插入 html - 您可以使用类名来完成此任务,如下所示:

html 文件:

 <div>
  <span>
    <ng-content select=".my-component-header">  // <--- Look Here :)
      <!-- optional header slot here -->
    </ng-content>
  </span>

  <div class="my-component-content">
    <ng-content>
      <!-- content slot here -->
    </ng-content>
  </div>
</div>
Run Code Online (Sandbox Code Playgroud)

你想像这样使用它:

<my-component>
  <span class="my-component-header">  // <--- Look Here :) 
     <!-- this is optional --> 
    <p>html content here</p>
  </span>


  <p>blabla content</p>
  <!-- other html -->
</my-component>
Run Code Online (Sandbox Code Playgroud)

所以...没有更多不存在的组件,因此没有问题,没有错误,不需要 app.module.ts 中的 CUSTOM_ELEMENTS_SCHEMA

因此,如果您像我一样并且不想将 CUSTOM_ELEMENTS_SCHEMA 添加到模块中 - 以这种方式使用您的组件不会产生错误并且更清晰。

有关此问题的更多信息 - https://github.com/angular/angular/issues/11251

有关 Angular 内容投影的更多信息 - https://blog.angular-university.io/angular-ng-content/

您还可以看到https://scotch.io/tutorials/angular-2-transclusion-using-ng-content