Angular中的ng-component是什么? [英] What IS ng-component in Angular?
问题描述
在angular中有两个非常相似的命名指令(或属性)
There are two very similar named directives (or attributes) in angular
ng-component
和 如果使用ng-component
代替ng-container
,则会出现各种奇怪的行为-例如自动插入<router-outlet>
.
If you put ng-component
instead of ng-container
you'll get all kinds of weird behavior - such as automatic insertion of a <router-outlet>
.
我经常想知道用户代码中ng-component
的目的是真的吗?
I've often wondered is there actually a purpose for ng-component
in user code.
这是一件遗留的东西吗?它仅是内部的吗?它可以解决任何问题吗?
Is it a legacy thing? Is it internal only? Does it solve any problems?
推荐答案
角度路由器使用路由器出口指令.除非指定了特定的路由器出口指令,否则Angular会自动将路由的组件放置在<ng-component>
元素内(即默认路由器出口指令).通常,您不直接使用此指令.您可以使用<router-outlet>
元素来调用它.换句话说,如果没有给出其他指令,ng-component
是注入元素的默认标签名称.
Angular routers render the components they have navigated to using a router outlet directive. Unless a specific router outlet directive is specified, Angular will automatically place the routed component within an <ng-component>
element (i.e., a default router outlet directive). You generally don't use this directive directly. You'd invoke it by using a <router-outlet>
element. In other words, ng-component
is the default tag name of the injected element, if another directive isn't given.
So yes, it is generally used internally. As stated here:
您可以将样式附加到"ng-component"选择器;但是,给定 父组件可能包含多个路由器出口的事实 元素(用于命名和未命名视图),提供唯一的本地 手柄将使CSS选择器更加直观.
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中的ng-component是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!