简体   繁体   中英

Angular 2 - MEAN MongoDB NodeJS

I'm using this GitHub example ( https://github.com/bradtraversy/mean_mytasklist ) and it works fine so far. I see the tasks of my MongoDB, can add, delete tasks.

Then I was trying to add a new component "header"

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

@Component({
  moduleId: module.id,
  selector: 'headers',
  templateUrl: 'header.component.html'
})
export class HeaderComponent {

}

I added this component in my app.modules.ts

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { HttpModule } from '@angular/http';
import { FormsModule } from '@angular/forms';
import { AppComponent } from './app.component';
import { TasksComponent } from './components/tasks/tasks.component';
import { HeaderComponent } from './components/header/header.component';

@NgModule({
  imports: [BrowserModule, HttpModule, FormsModule],
  declarations: [AppComponent, TasksComponent, HeaderComponent],
  bootstrap: [AppComponent]
})
export class AppModule { }

In my app.component.html I am using this selector.

<div class="container">
    <headers></headers>
    <h1>MyTaskList</h1>
    <hr>
    <tasks></tasks>
</div>

After refreshing my browser I'll get this error message:

zone.js:420 Unhandled Promise rejection: Template parse errors: 'headers' is not a known element: 1. If 'headers' is an Angular component, then verify that it is part of this module. 2. If 'headers' is a Web Component then add "CUSTOM_ELEMENTS_SCHEMA" to the '@NgModule.schemas' of this component to suppress this message. (" [ERROR ->]

<headers></headers>
<h1>MyTaskList</h1>
<hr>

Any ideas? Many thanks!!!

"): AppComponent@1:4 ; Zone: ; Task: Promise.then ;

Which version of Angular 2 you are using & You are not using webpack for build ?

The solution is mentioned in the error console itself. You need to add

schemas: [CUSTOM_ELEMENTS_SCHEMA ] 

So your app.module.ts should look something like this,

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

@NgModule({
  imports: [BrowserModule, HttpModule, FormsModule],
  declarations: [AppComponent, TasksComponent, HeaderComponent],
  bootstrap: [AppComponent],
   schemas: [CUSTOM_ELEMENTS_SCHEMA ] 
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM