我在学习Angular 2.我有个问题。 我需要app.component.ts吗?我有很多在文件夹中的组件。该文件夹的每个人都包含组件和模板,但我想知道我是否真的需要主要组件或我可以删除它?Angular 2中需要app.component.ts吗?
此致敬礼。
我在学习Angular 2.我有个问题。 我需要app.component.ts吗?我有很多在文件夹中的组件。该文件夹的每个人都包含组件和模板,但我想知道我是否真的需要主要组件或我可以删除它?Angular 2中需要app.component.ts吗?
此致敬礼。
请记住:至少有一个模块和部件都需要启动Angular2应用。
我需要app.component.ts吗?
没有必要。 这只是一个名称.ts文件。它可以是任何其他组件。不过据说至少需要一个模块和组件才能启动Angular2 App。
了解下面的东西,
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 {}
你应该有一个根组件来引导你的应用程序。
@ NgModule.bootstrap属性将此AppComponent标识为引导组件。当角启动应用程序,它会将AppComponent的HTML渲染的DOM [https://angular.io/docs/ts/latest/guide/ngmodule.html#!#bootstrap]
大回答谢谢! –
谢谢你,很高兴你喜欢它。 – micronyks
@micronyks那么,我们可以将app.module.ts和app.component.ts重命名为其他内容,并在main.ts中进行必要的更改吗? –