Angular根组件app详解

 

app.module.ts

//根模块,告诉angular如何组装应用

//浏览器解析的模块
import { BrowserModule } from '@angular/platform-browser';
//angular核心模块:导入该模块是因为@NgModule()装饰器需要该模块
import { NgModule } from '@angular/core';
//根组件(包括app.component.ts,.html,.css三个文件)
import { AppComponent } from './app.component';
import { NewsComponent } from './components/news/news.component';

@NgModule({
  //声明:所有组件都必须要在模块中声明(组件、指令、管道),若通过ng g component 等命令创建的,会自动在这里添加声明
  declarations: [
    AppComponent, //根组件
    NewsComponent
  ],
  //导入需要的其它模块(模块)
  imports: [
    BrowserModule
  ],
  //提供给整个模块的服务写在这(服务)
  providers: [],
  //当其它模块导入这个模块时,提供给其它模块使用到的组件需要在这里声明,但其实这里不需要这个属性,因为根模块不用给任何模块导入(组件模板)
  exports: [],
  //应用的主视图:只有根模块需要这个属性
  bootstrap: [AppComponent]
})
export class AppModule { }

app.component.ts

//引入angular核心模块中的Component,因为下面需要使用到@Component()装饰器
import { Component } from '@angular/core';

@Component({
  selector: 'app-root',//组件对外的名字
  templateUrl: './app.component.html',//组件的模板
  styleUrls: ['./app.component.css']//组件的样式文件
})
export class AppComponent {
  title = 'angularDemo111111';
}

app.component.html

<!--使用其他组件:直接通过组件的selector作为标签名即可-->
<app-news></app-news>

app.component.css

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值