Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Minimum definition of a component Angular 4

Tags:

angular

What is the minimum definition of a component, After looking at https://angular.io/api/core/Component it seems all are optional. Can some one explain what is the minimum definition.

like image 365
Rashmi Kumari Avatar asked Dec 11 '25 14:12

Rashmi Kumari


2 Answers

The absolute minimal configuration for a @Component in angular is a template. Both template properties are set to optional because you have to define either template or templateUrl.

When you don't define them, you will get an exception like this;

No template specified for component 'ComponentName'

A selector property is not required, as you can also use your components in a route.

like image 51
Nico Van Belle Avatar answered Dec 14 '25 06:12

Nico Van Belle


Minimum configuration for Component is a template,

  1. constructor is not required, it is auto generated
  2. selector is also optional , component can be accessed via router by class name
  3. template is required (either template or templateUrl)

    import { Component } from '@angular/core';
    @Component({
      templateUrl: './minimum.component.html' // or
      template: '' 
    })
    export class MinimumComponent {}
    
like image 35
Nitin Lawande Avatar answered Dec 14 '25 05:12

Nitin Lawande



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!