angular 中如何使用自定义组件


1.创建header组件

ng g component components/header

header.component.ts

import { Component, OnInit } from '@angular/core'; /*引入 angular 核心*/

@Component({
  selector: 'app-header', /*使用这个组件的名称*/
  templateUrl: './header.component.html', /*html 模板*/
  styleUrls: ['./header.component.scss'] /*css 样式*/
})
export class HeaderComponent implements OnInit { /*实现接口*/

  constructor() { }/*构造函数*/

  ngOnInit() {/*初始化加载的生命周期函数*/
  }

}

 

2.在使用的地方使用组件

比如在跟组件下使用

<!--The content below is only a placeholder and can be replaced.-->
<div style="text-align:center">


  <h1>
    Welcome to {{ title }}
  </h1>
</div>
<app-header></app-header>
<router-outlet></router-outlet>

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM