问题
There are two very similar named directives (or attributes) in angular
ng-component
and ng-container
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>
.
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?
回答1:
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:
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.
来源:https://stackoverflow.com/questions/57702828/what-is-ng-component-in-angular