想了解Angular2KarmaTest'component-name'不是一个已知元素的新动态吗?本文将为您提供详细的信息,我们还将为您解答关于angularisnotdefined的相关问题,此外
想了解Angular 2 Karma Test'component-name'不是一个已知元素的新动态吗?本文将为您提供详细的信息,我们还将为您解答关于angular is not defined的相关问题,此外,我们还将为您介绍关于Angular 2 Components(Angular 2 组件电子书教程免费下载)、Angular 2“组件”不是已知元素、Angular 2:从Component访问一个元素,getDocumentById不起作用、Angular 2:如何在该Component中访问自定义Component的FormControl实例?的新知识。
本文目录一览:- Angular 2 Karma Test'component-name'不是一个已知元素(angular is not defined)
- Angular 2 Components(Angular 2 组件电子书教程免费下载)
- Angular 2“组件”不是已知元素
- Angular 2:从Component访问一个元素,getDocumentById不起作用
- Angular 2:如何在该Component中访问自定义Component的FormControl实例?
Angular 2 Karma Test'component-name'不是一个已知元素(angular is not defined)
在AppComponent中,我在HTML代码中使用了nav组件。用户界面看起来不错。服务时没有错误。当我查看应用程序时,控制台中没有错误。
但是,当我为我的项目运行Karma时,出现了一个错误:
Failed: Template parse errors: ''app-nav'' is not a known element:1. If ''app-nav'' is an Angular component, then verify that it is part of this module.2. If ''app-nav'' is a Web Component then add ''CUSTOM_ELEMENTS_SCHEMA'' to the ''@NgModule.schemas'' of this component to suppress this message.
在我的 app.module.ts中 :
有:
import { NavComponent } from ''./nav/nav.component'';
它也在NgModule的声明部分中
@NgModule({ declarations: [ AppComponent, CafeComponent, ModalComponent, NavComponent, NewsFeedComponent ], imports: [ BrowserModule, FormsModule, HttpModule, JsonpModule, ModalModule.forRoot(), ModalModule, NgbModule.forRoot(), BootstrapModalModule, AppRoutingModule ], providers: [], bootstrap: [AppComponent]})
我NavComponent
在我的AppComponent
app.component.ts
import { Component, ViewContainerRef } from ''@angular/core'';import { Overlay } from ''angular2-modal'';import { Modal } from ''angular2-modal/plugins/bootstrap'';import { NavComponent } from ''./nav/nav.component'';@Component({ selector: ''app-root'', templateUrl: ''./app.component.html'', styleUrls: [''./app.component.css'']})export class AppComponent { title = ''Angela'';}
app.component.html
<app-nav></app-nav><div></div>
我已经看到了类似的问题,但是该问题的答案表明我们应该在具有导出功能的nav组件中添加NgModule,但是这样做时会出现编译错误。
还有: app.component.spec.ts
import {NavComponent} from ''./nav/nav.component'';import { TestBed, async } from ''@angular/core/testing'';import { AppComponent } from ''./app.component'';
答案1
小编典典因为在单元测试中,您想测试大部分与应用程序其他部分隔离的组件,所以默认情况下,Angular不会添加模块的依赖项,例如组件,服务等。因此,您需要在测试中手动执行此操作。基本上,这里有两个选择:
A)在测试中声明原始NavComponent
describe(''AppComponent'', () => { beforeEach(async(() => { TestBed.configureTestingModule({ declarations: [ AppComponent, NavComponent ] }).compileComponents(); }));
B)模拟导航组件
describe(''AppComponent'', () => { beforeEach(async(() => { TestBed.configureTestingModule({ declarations: [ AppComponent, MockNavComponent ] }).compileComponents(); }));// it(...) test cases});@Component({ selector: ''app-nav'', template: ''''})class MockNavComponent {}
您可以在官方文档中找到更多信息。
Angular 2 Components(Angular 2 组件电子书教程免费下载)
Angular 2 组件电子书教程
下载地址:AngularJS Directives Cookbook.pdf
更多 Angular 电子书、React 电子书、Vue.js 电子书,请关注我的简书主页。
Angular 2“组件”不是已知元素
我正在尝试在其他模块中使用我在 AppModule 中创建的组件。我收到以下错误:
“未捕获(承诺):错误:模板解析错误:
‘contacts-box’ 不是已知元素:
- 如果 ‘contacts-box’ 是一个 Angular 组件,那么验证它是这个模块的一部分。
- 如果“contacts-box”是一个 Web
组件,则将“CUSTOM_ELEMENTS_SCHEMA”添加到该组件的“@NgModule.schemas”以禁止显示此消息。
我的项目结构很简单:
我将我的页面保存在 pages
目录中,每个页面保存在不同的模块中(例如客户模块),每个模块都有多个组件(如客户列表组件、客户添加组件等)。我想在这些组件中使用我的
ContactBoxComponent(例如在客户添加组件中)。
如您所见,我在小部件目录中创建了联系人框组件,因此它基本上位于 AppModule 中。我将 ContactBoxComponent 导入添加到
app.module.ts 并将其放入 AppModule 的声明列表中。它没有用,所以我搜索了我的问题并将 ContactBoxComponent
添加到导出列表中。没有帮助。我还尝试将 ContactBoxComponent 放入 CustomersAddComponent
中,然后放入另一个(来自不同的模块),但我收到一个错误,说有多个声明。
我错过了什么?
答案1
小编典典这些是我遇到此类错误时执行的 5 个步骤。
- 你确定名字是对的吗?(还要检查组件中定义的选择器)
- 在模块中声明组件?
- 如果它在另一个模块中,导出组件?
- 如果它在另一个模块中,导入那个模块?
- 重启cli?
在单元测试期间发生错误时,请确保您声明了组件或导入了模块TestBed.configureTestingModule
我还尝试将 ContactBoxComponent 放入 CustomersAddComponent
中,然后放入另一个(来自不同的模块),但我收到一个错误,说有多个声明。
您不能两次声明一个组件。您应该在一个新的单独模块中声明和导出您的组件。接下来,您应该在要使用组件的每个模块中导入这个新模块。
很难说什么时候应该创建新模块,什么时候不应该。我通常为我重用的每个组件创建一个新模块。当我有一些几乎在任何地方都使用的组件时,我将它们放在一个模块中。当我有一个不重复使用的组件时,我不会创建一个单独的模块,直到我在其他地方需要它。
尽管将所有组件放在一个模块中可能很诱人,但这对性能不利。在开发过程中,每次进行更改时,模块都必须重新编译。模块越大(组件越多),花费的时间就越多。对大模块进行小改动比在小模块中进行小改动需要更多时间。
Angular 2:从Component访问一个元素,getDocumentById不起作用
< div id =“myId”>通过它的身份.我尝试在我的组件(TypeScript)中执行:document.getElementById(“myId”),但是我收到一个错误:“找不到名称文档”.我看到在其他帖子中我们可以使用@ViewChild做类似的事情,但我不知道我们如何使用div,任何想法?
模板
<div id ="myId" #myId>
零件
import { Component,ViewChild,ElementRef,AfterViewInit } from '@angular/core'; @Component({ selector: 'app-root',templateUrl: './app.component.html',styleUrls: ['./app.component.css'] }) export class AppComponent implements AfterViewInit { @ViewChild('myId') myId: ElementRef; constructor() { } ngAfterViewInit() { console.log(this.myId.nativeElement); } }
This blog post by Kara Erickson对于熟悉Angular处理这类事情的方法非常好.
Angular 2:如何在该Component中访问自定义Component的FormControl实例?
<my-control name="something" [(ngModel)]="model.something" required></my-control>
而不是每次都重复这个:
<divhttps://www.jb51.cc/tag/Feed/" target="_blank">Feedback" [ngClass]="{'has-success': someInput.valid,'has-error': someInput.invalid && someInput.dirty}"> <labelfor="someId">{{label || 'Some Input'}}</label> <input type="test"id="someId" placeholder="Some Input" [ngModel]="value" (ngModel)="onChange($event)" name="someInput" required #someInput="ngModel" minlength="8"/> <spanhttps://www.jb51.cc/tag/Feed/" target="_blank">Feedback" aria-hidden="true" [ngClass]="{'glyphicon-ok': someInput.valid,'glyphicon-remove': someInput.invalid && someInput.dirty}"></span> <div [hidden]="someInput.valid || someInput.pristine || !someInput.errors.required">Some Input is required</div> <div [hidden]="someInput.valid || someInput.pristine || !someInput.errors.minlength">Some Input must be at least 8 characters</div> </div>
所以我通过自定义组件实现了以下有关如何在线创建自定义组件的文章:
https://blog.thoughtram.io/angular/2016/07/27/custom-form-controls-in-angular-2.html
缺少的是能够将验证移出组件,但允许自定义组件处理该验证的显示.因此,如果你看一下我的目的是允许组件的用户指定验证而不是让组件强加特定的验证(注意某些验证是组件固有的,例如电子邮件地址组件会验证它是一封没有用户指定的电子邮件).请注意,必需的是该客户组件的使用情况.
那么如何在该组件的定义中获得对自定义组件的FormControl的引用?注意:我理解如何访问模板的FormControl实例中的输入字段,因为上面的代码完全证明了这一点.我要求的是模板所在的自定义控件的FormControl实例.在我引用的文章中,它将是CounterInputComponent的FormControl.
解决方法
@ViewChild(NgModel) model: NgModel;
然后你可以通过以下方式访问FormControl:
this.model.control
今天关于Angular 2 Karma Test'component-name'不是一个已知元素和angular is not defined的讲解已经结束,谢谢您的阅读,如果想了解更多关于Angular 2 Components(Angular 2 组件电子书教程免费下载)、Angular 2“组件”不是已知元素、Angular 2:从Component访问一个元素,getDocumentById不起作用、Angular 2:如何在该Component中访问自定义Component的FormControl实例?的相关知识,请在本站搜索。
本文标签: