一、创建组件

1.创建一个 news组件
ng g component [组件目录/组件名称]

ng g component components/news

Angular:创建使用组件(二)
2.在VS Code中查看项目结构
Angular:创建使用组件(二)
3.如果通过命令行创建组件,将自动在 app.module.ts 中引入
Angular:创建使用组件(二)

二、使用组件

1.在 app.component.html 中添加 news 组件

<!--The content below is only a placeholder and can be replaced.-->
    <app-news></app-news>

2.在 news.component.html 中添加内容

<h1>新闻组件</h1>

3.运行项目

ng serve --open

Angular:创建使用组件(二)

相关文章: