如何在 angular 中的特定位置调用另一个内部的组件

how to call a component inside another in a specific position in angular

我在 angular 中有两个组件,如下所示:

mycomponent.component.html

<app-mycomponent2>
<p> this is a text </p>
</app-mycomponent2>

mycomponent2.component.html

<div class="bg-blue">
<!-- the code of first component should be added here-->
</div>

现在我不知道如何让我的第二个组件在特定位置调用代码,因为我将在我的组件中多次使用这段代码,我想避免 copy/paste使用我描述的结构。

我不确定你所说的 "call the code," 是什么意思,但听起来你对 "content projection." 感兴趣 你使用 <ng-content> 标签来包含随后将"projected" 到您的组件中进行渲染。在您的情况下,<p>this is a text</p> 内容将通过 <ng-content> 标记投射到 mycomponent2 中。

这是一个很好的投影示例: https://codecraft.tv/courses/angular/components/content-projection/