如何在 Angular 中使用内容投影(又名嵌入)

编程入门 行业动态 更新时间:2024-10-26 12:30:38
本文介绍了如何在 Angular 中使用内容投影(又名嵌入)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我是 angular 4 的新手.

我对 angular 4 嵌入式组件有疑问.

示例:

<英雄列表><英雄>​​</英雄><英雄>​​</英雄></英雄列表>

我想知道如何基于这种将组件嵌入另一个组件中的结构创建视图.

解决方案

你应该在 hero-list 中使用 </ng-content>代码>-组件.这样你就可以实现上面的愿望了.

hero-listponent.html

<h1>英雄列表</h1><ng-content></ng-content>

现在你可以包装你的 hero-item-components,它们将被打印在 hero-listcomponents 内.

appponent.html

<英雄列表><英雄物品></英雄物品><英雄物品></英雄物品></英雄列表>

这是工作示例:https://stackblitz/edit/angular-nvpmtc

而且这里 是一篇关于内容投影的好文章角度.

I am new to angular 4.

I have a doubt in angular 4 embedded components.

example:

<hero-list>
    <hero></hero>
    <hero></hero>
</hero-list>

I wanted to know how to create a view based on this structure that is embedding component inside another component.

解决方案

You should use <ng-content></ng-content> in your hero-list-component. So you can realize your wish above.

hero-listponent.html

<div class="hero-list">
  <h1>Hero list</h1>
  <ng-content></ng-content>
</div>

And now you can wrap your hero-item-components and they will be printed inside of hero-listcomponent.

appponent.html

<hero-list>
  <hero-item></hero-item>
  <hero-item></hero-item>
</hero-list>

Here is working example: https://stackblitz/edit/angular-nvpmtc

And here is a good article about content projection in angualr.

这篇关于如何在 Angular 中使用内容投影(又名嵌入)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-22 12:25:41,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1023510.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:又名   内容   如何在   Angular

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!