-
ng g --help (命令帮助)
-
ng g component components/home (创建组件命令)
-
创建的组件包括:
home.component.css
home.component.html
home.component.spec.ts
home.component.ts
- home.component.ts文件:
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-home', // 组件名称,<app-home></app-home>
templateUrl: './home.component.html',
styleUrls: ['./home.component.css']
})
export class HomeComponent implements OnInit {
constructor() { }
ngOnInit() {
}
}
- 会在app.module.ts中引用该组件
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppComponent } from './app.component';
// 引用新建的组件
import { HomeComponent } from './components/home/home.component';
@NgModule({
declarations: [
AppComponent,
HomeComponent // 配置引用的组件
],
imports: [
BrowserModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
- 使用该组件
<app-home></app-home>