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/