What IS ng-component in Angular?

空扰寡人 提交于 2020-05-14 19:59:15

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!