Do I need app.component.ts in Angular 2?

I am studying Angular 2. I have a question. Do I need app.component.ts? I have many components that are in folders. All folders contain a component and a template, but I wonder if I really need the main component, or can I remove it?

Sincerely.

+4
source share
2 answers

Keep in mind: To launch the Angular2 App.

At least one module and component is required.

Do I need app.component.ts?

Not required . This is simply the name of the .ts file . It can be any other component. But, as said, at least one module and component must initiate the Angular2 App.

Understand below

main.ts

import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { AppModule } from './app.module';   //<<<==== it imports AppModule class from app.module.ts file
const platform = platformBrowserDynamic();
platform.bootstrapModule(AppModule);        //<<<===we bootstarp our AppModule here

app.module.ts// .ts

//contents are important as it contains @NgModule({}) decorator.

import { NgModule }      from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { SomeComponent }   from './some.component';  
                                           //<<<===we import SomeComponent class from some.component.ts file to bootstrap it.


@NgModule({
  imports:      [ BrowserModule],
  declarations: [ SomeComponent ],         
  bootstrap:    [ SomeComponent ]          //<<<===we are going to bootstrap/initialize SomeComponent as our first component.
})
export class AppModule { }                 //<<<====we imported AppModule (contains @NgModule decorator) in main.ts as we are going to bootstrap this class which has @NgModule() decorator.

some.component.ts// .ts

import { Component } from '@angular/core';
import {UserService} from '../shared/shared.service';
@Component({
  selector: 'my-app',                       //<<<===make sure this matches with custom HTML tag used in index.html
  template: `<h1>Anglar2</h1>  
  `
})
export class SomeComponent {}
+10

.

@ NgModule.bootstrap AppComponent . Angular , HTML- AppComponent DOM [https://angular.io/docs/ts/latest/guide/ngmodule.html#!#bootstrap]

0

Source: https://habr.com/ru/post/1655705/


All Articles