I am trying to import component from one file another root component file. it give error as ..
zone.js:484 Unhandled Promise rejection: Template parse errors: 'courses' is not a known element: 1. If 'courses' is an Angular component, then verify that it is part of this module. 2. If 'courses' is a Web Component then add "CUSTOM_ELEMENTS_SCHEMA" to the '@NgModule.schema' of this component to suppress this message. ("
My First Angular 2 App
[ERROR ->]"): AppComponent@0:31 ; Zone: ; Task: Promise.then ; Value: Error: Template parse errors:(…) Error: Template parse errors: 'courses' is not a known element:
My app.component.ts be like [root component file]
import { Component } from '@angular/core'; import {CoursesComponent} from './courses.component'; @Component({ selector: 'my-app', template: '<h1>My First Angular 2 App</h1><courses></courses>', directives:[CoursesComponent], }) export class AppComponent { }
and my courses.component.ts be ;
import { Component } from '@angular/core'; @Component({ selector: 'courses', template: '<h1>courses</h1>' }) export class CoursesComponent { }
while importing courses component from courses.component.ts file inside app.component i am not able declare directive inside @Component{}
directives:[CoursesComponent]
giving error to me
Please advise solution over it.
Angular 2 uses ES6 style syntax which allows to reuse code by importing them from one file to another.
Component can be declared in a single module only. In order to use a component from another module, you need to do two simple tasks: Export the component in the other module. Import the other module, into the current module.
The Angular framework allows us to use a component within another component and when we do so then it is called Angular Nested Components. The outside component is called the parent component and the inner component is called the child component.
You can put commonly used directives, pipes, and components into one module and then import just that module wherever you need it in other parts of your application. Notice the following: It imports the CommonModule because the module's component needs common directives.
This happens by a combination of the root module and the root component working together. The root component is the very first component that is referenced and hosted in the main index.html web page. Everything else in Angular builds off of this root, so it helps to take a close look at how this root component works.
For Angular RC5 and RC6 you have to declare component in the module metadata decorator's declarations key, so add CoursesComponent in your main module declarations as below and remove directives from AppComponent metadata.
Run your Angular application from the terminal using the ng serve command, and open your website in a web browser. Since the component is re-usable, there are attributes that you may want to change every time you use it. In this case, you can use input parameters. Then add two input variables inside the UICardComponent class.
The app component is a root component generated by Angular itself. We have added app-register component in app.component.html with the properties tableDataValues. Data will return from the app-register component by submitting values.
You should declare it with declarations array(meta property) of @NgModule
as shown below (RC5 and later
),
import {CoursesComponent} from './courses.component'; @NgModule({ imports: [ BrowserModule], declarations: [ AppComponent,CoursesComponent], //<----here providers: [], bootstrap: [ AppComponent ] })
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With