Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to import component into another root component in Angular 2

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.

like image 715
Vishu Avatar asked Sep 09 '16 11:09

Vishu


People also ask

Can I import one component into another Angular?

Angular 2 uses ES6 style syntax which allows to reuse code by importing them from one file to another.

How do I use a component in another module?

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.

Can I create a component inside another component Angular?

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.

Can we use one module component to other module components?

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.

How does the root module work in angular?

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.

How to add coursescomponent in Angular 5?

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.

How to add input variables to an angular component?

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.

How to return data from the app component in angular?

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.


1 Answers

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 ] }) 
like image 159
Nikhil Shah Avatar answered Sep 20 '22 02:09

Nikhil Shah