加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

我在Angular 2中需要app.component.ts吗?

发布时间:2020-12-17 06:53:30 所属栏目:安全 来源:网络整理
导读:我正在学习Angular 2.我有一个问题. ?我需要app.component.ts吗?我有很多组件在文件夹中.每个文件夹都包含组件和模板,但我想知道我真的需要主要组件或我可以删除它吗? 最好的祝福. 解决方法 请记住:启动Angular2 App至少需要一个模块和组件. Do I need ap
我正在学习Angular 2.我有一个问题.
?我需要app.component.ts吗?我有很多组件在文件夹中.每个文件夹都包含组件和模板,但我想知道我真的需要主要组件或我可以删除它吗?

最好的祝福.

解决方法

请记住:启动Angular2 App至少需要一个模块和组件.

Do I need 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 {}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读