angular - Angular 中的 ng-component 是什么?

标签 angular ng-container

Angular中有两个非常相似的命名指令(或属性)

ng-component ng-container

如果你把 ng-component而不是 ng-container你会得到各种奇怪的行为——比如自动插入 <router-outlet> .

我经常想知道 ng-component 是否真的有目的?在用户代码中。

它是一个遗产吗?只是内部的吗?它可以解决任何问题吗?

最佳答案

Angular 路由器使用 router outlet directive 渲染它们导航到的组件。 .除非指定了特定的路由导出指令,否则 Angular 会自动将路由组件放置在 <ng-component> 中。元素(即默认路由器导出指令)。您通常不直接使用此指令。你可以使用 <router-outlet> 来调用它。元素。换句话说,ng-component是注入(inject)元素的默认标签名称,如果没有给出另一个指令。

所以是的,它通常在内部使用。如前所述here :

You could attach styles to the "ng-component" selector; however, given the fact that a parent component may contain multiple router-outlet elements (for named and unnamed views), providing a unique local handle would make CSS selectors a bit more intuitive.

关于angular - Angular 中的 ng-component 是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57702828/

相关文章:

html - 如何在单独的文件中加载 ng-template?

Angular 8 : Not Showing the content of ng-tmplate in nested structure

Angular 11+ MatDialog : inner component's (ngComponentOutlet) html does not trigger <button (click) ="close()"> method. 适用于 Angular 9

Angular 6 表单数组无法在 ng-container 内访问

angular - 循环遍历@ViewChildren 原生元素

javascript - Angular 6 : ERROR TypeError: Cannot read property 'toLowerCase' of undefined

javascript - 在 Angular 中,如何确定事件路由?

javascript - 如何使用 Jasmine 测试 Angular 6 中包含在 ng-container 中的元素?

html - 如何在 Angular 2 中轻轻更改 img src

javascript - Angular 中的异步操作完成