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)
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)
到你的模块.
干杯,拉斐尔
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)
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 {}
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
现在它有效.
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中.
对于包含 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 中,请确保您使用的任何新自定义元素的名称中都有“破折号”,例如。或等等
这是一篇相当长的文章,它对这个问题给出了更详细的解释。
当您有多槽内容投影时,问题(就我而言)就出现了
另请参阅内容投影了解更多信息。
例如,如果您有一个如下所示的组件:
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
有关此问题的更多信息 - 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
| 归档时间: |
|
| 查看次数: |
187340 次 |
| 最近记录: |