我是Angular2的新手。我试图创建一个组件,但显示一个错误。
这是app.component.ts文件。
import { Component } from '@angular/core';
import { MyComponentComponent } from './my-component.component';
@Component({
selector: 'my-app',
template: `
<h1>Hello {{name}}</h1>
<h4>Something</h4>
<my-component></my-component>
`,
directives: [MyComponentComponent]
})
export class AppComponent { name = 'Sam' }
这是我要创建的组件。
import { Component } from '@angular/core';
@Component({
selector: 'my-component',
template: `
<p>This is my article</p>
`
})
export class MyComponentComponent {
}
显示两个错误:
如果my-component是一个Angular组件,那么验证它是否是这个模块的一部分。
如果my-component是Web组件,那么将CUSTOM_ELEMENTS_SCHEMA添加到@NgModule中。模式来抑制此消息。
请帮助。
在我的例子中,我正在为一个使用子组件的组件编写单元测试,并且我正在编写测试以确保这些子组件在模板中:
it('should include the interview selection subview', () => {
expect(fixture.debugElement.query(By.css('app-interview')))
.toBeTruthy()
});
我没有得到一个错误,但一个警告:
警告:“应用面试”不是一个已知的元素:
如果'app-interview'是一个Angular组件,那么验证它是这个模块的一部分。警告:“应用面试”不是一个已知的元素:
如果'app-interview'是一个Angular组件,那么验证它是这个模块的一部分。
如果'app-interview'是一个Web组件,那么将'CUSTOM_ELEMENTS_SCHEMA'添加到'@NgModule '中。这个组件的模式
为了压制这条信息。”
此外,在测试期间,子组件没有在浏览器中显示。
我使用ng g c newcomponent来生成所有的组件,所以它们已经在appmodule中声明了,而不是我正在指定的组件的测试模块。
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ EditorComponent,
InterviewComponent]
})
.compileComponents();
}));
在你的components.module.ts中,你应该像这样导入IonicModule
这样的:
import {IonicModule} from '@ionic/angular';
然后像这样导入IonicModule:
imports: [
CommonModule,
IonicModule
],
所以你的components.module.ts会像这样:
import { CommonModule } from '@angular/common';
import {PostComponent} from './post/post.component'
import { IonicModule } from '@ionic/angular';
@NgModule({
declarations: [PostComponent],
imports: [
CommonModule,
IonicModule
],
exports: [PostComponent]
})
export class ComponentsModule { }```
不要导出默认的MyComponentComponent类!
与标题相关的问题,如果不是具体的问题:
我不小心做了一个…
export default class MyComponentComponent {
... 这也把事情搞砸了。
为什么?当我把它放入声明时,VS Code添加了导入到我的模块,但不是…
import { MyComponentComponent } from '...';
它有
import MyComponentComponent from '...';
这并没有向上映射到下游,假设因为它没有在默认导入的任何地方“真正”命名。
export class MyComponentComponent {
没有违约。利润。