Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I'm learning Angular 2. I've a question. Do I need app.component.ts ? I have a lot of components which are in folders. Everyone of the folders contains component and template but I'm wondering Do I really need main component or I could remove it ?

Best regards.

like image 905
Petar Petrov Avatar asked Sep 24 '16 16:09

Petar Petrov


People also ask

What is the app component TTS file in an angular application?

What is the app.components.ts file in an Angular application? Angular is an open-source platform and a JavaScript framework written in TypeScript for building single-page applications. Angular building blocks are components. The file structure that is created by default in the Angular application components consists of the following files:

What are components in angular?

Components are basically classes that interact with the .html file of the component, which gets displayed on the browser. We have seen the file structure in one of our previous chapters. The file structure has the app component and it consists of the following files −. app.component.css. app.component.html.

What are some angular 2 and Angular 4 issues?

Angular 2 Karma Test 'component-name' is not a known element 13 Angular4 multiple modules issue 14 routing navigate method not redirecting to targeted component 10

What is appcomponent export in Angular JS?

We are defining a class called AppComponent. The export keyword is used so that the component can be used in other modules in the Angular JS application. appTitle is the name of the property. The property is given the type of string.


2 Answers

Keep in mind : At least one module and component are required to initiate Angular2 App.

Do I need app.component.ts?

Not necessary. That is just a name of .ts file. It can be any other component. But as said at least one module and component are required to initiate Angular2 App.

Understand below things,

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 // name of .ts file

//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 //name of .ts file

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 {}
like image 187
Nikhil Shah Avatar answered Oct 17 '22 02:10

Nikhil Shah


you should have one root component to bootstrap your application.

@NgModule.bootstrap property identifies this AppComponent as the bootstrap component. When Angular launches the app, it places the HTML rendering of AppComponent in the DOM [https://angular.io/docs/ts/latest/guide/ngmodule.html#!#bootstrap]

like image 1
manish.nith Avatar answered Oct 17 '22 01:10

manish.nith