Loading…

How to create a component in Angular 2?

  • Import the top-level component using import { Component } from '@angular/core';
  • Describe the component using the @ symbol. Create instances of @Directive, @Injectable, @RouterConfig etc.
  • Add meta-data like selector, providers, styles, template.
  • Export the component
  • Bootstrap the component

Example

                                        

import { Component } from '@angular/core';
import { bootstrap } from '@angular/platform-browser-dynamic';

@Component({
selector: 'my-test',
styles: [`
h1 {
color:#000000;
padding:5px;
}
`]
template: `
<h1>Hi There! This is {{componentName}}.</h1>
`
})
export class TestComponent {
componentName: 'TestComponent'
}
bootstrap(TestComponent);

Suggest An Answer

No suggestions avaliable!

Latest post from Angular 2 Interview Questions

Ask Question