我是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中。模式来抑制此消息。

请帮助。


当前回答

我有一个类似的问题,但没有一个提到的解决方案工作。最后我意识到我有一个路由问题:

我想如果我在应用程序路由中指定我的组件。模块如下:

import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { MainComponent } from './pages/main/main.component';

const routes: Routes = [
  {
    path: '', 
    component: MainComponent
  },
]

@NgModule({
  imports: [RouterModule.forRoot(routes)],
  exports: [RouterModule]
})
export class AppRoutingModule { }

我将不必在我的AppModule中导入MainModule,这是错误的,因为这里只有组件被导入。MainModule中声明的任何其他组件将不可见,这将导致您所描述的错误消息。

解决方案1: 导入声明模块 解决方案2: 惰性加载组件,如下所示:

const routes: Routes = [
{
    path: '', loadChildren: () => import('path_to_your_declaringModule').then(m => m.declaringModule)},
}

这是可行的,因为整个模块都导入到这里。

我意识到你在你的例子中没有使用路由,只是把这个发布给其他可能使用路由并在搜索解决方案时偶然发现这个问题的人,就像我一样。

其他回答

你是否像这样在app.module.ts中导入它并删除指令位:-

@NgModule({
    bootstrap: [AppComponent],
    imports: [MyComponentModule],// or whatever the name of the module is that declares your component.

    declarations: [AppComponent],
    providers: []
})
export class AppModule {}

你的MyComponentModule应该是这样的

@NgModule({
    imports: [],
    exports: [MyComponentComponent],
    declarations: [MyComponentComponent],
    providers: [],
})
export class MyComponentModule {
}

在我的例子中,我正在为一个使用子组件的组件编写单元测试,并且我正在编写测试以确保这些子组件在模板中:

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();
  }));

这可能是html标签组件的名称

你可以在html中使用这样的东西<mycomponent></mycomponent>

你必须使用这个<app-mycomponent></app-mycomponent>

转到tsconfig。json文件。把这段代码写在angularCompilerOption下:

 "angularCompilerOptions": {
     "fullTemplateTypeCheck": true,
     "strictInjectionParameters": true,
     **"enableIvy": false**
 }

我有不同的模块除了app.module,例如主页。模块

我忘了把Home-page.component添加到Home-page。模块