我正在从新的Angular 17文档中学习Angular 多内容投影. 当我使用文档中的示例时,我收到错误:

配置文件.组件.html::

<div class="red">
  <ng-content select="profile-header"></ng-content>
</div>

<div class="green">
  <ng-content></ng-content>
</div>

<div class="blue">
  <ng-content select="profile-footer"></ng-content>
</div>

在app.component.html::

<app-profile>
    <profile-header><h2>Header 1</h2></profile-header>
    <p>This is projected content</p>
</app-profile>

我收到此错误::

NG8001: 'profile-header' is not a known element:

我怎么才能解决它呢?

推荐答案

SELECT等同于javascript查询 Select 器([<<attribute name>>]),因此您可以这样做属性 Select 器.

App.component.html

<app-profile>
    <h2 header>Header 1</h2>
    <p>This is projected content</p>
    <h2 footer>Footer 1</h2>
</app-profile>

Profile.Component.html:

<div class="red">
  <ng-content select="[header]"></ng-content>
</div>

<div class="green">
  <ng-content></ng-content>
</div>

<div class="blue">
  <ng-content select="[footer]"></ng-content>
</div>

docs here

Angular相关问答推荐

尾风手风琴中的Ngor

MAT表的内联文本编辑-未刷新编辑图标

第15角Cookie的单元测试用例

在以下位置升级到Angular 16 Break Sharepoint广告:This._history.replaceState不是函数

Angular-每2个流式传输多个HTTP调用

Angular CLI 与 Angular 版本不兼容

升级到 Webpack 5.79.0 后 NX Angular 项目构建失败

如何在 Angular 中将tailwind 类传递给 fontawesome

多个 Mat Paginator 在 Angular 组件中不起作用

有条件地取消所有内部可观察对象或切换到仅发出一个值的新对象

如何在Angular Material2中获取活动选项卡

Angular2订阅对子组件中@Input的更改

Angular 7 测试:NullInjectorError:No provider for ActivatedRoute

在 Angular 2 中打印 Html 模板

Angular 2 中的 OnChanges 和 DoCheck 有什么区别?

Angular 4 Bootstrap 下拉菜单需要 Popper.js

@HostBinding 与 Angular 中的变量类

Angular ngClass 和单击事件以切换类

路由 getCurrentNavigation 始终返回 null

在渲染视图/模板之前等待 Angular 2 加载/解析模型