Angular以哪种方式解析重复的指令/组件选择器? [英] In which way does Angular resolve duplicate directive/component selectors?

查看:70
本文介绍了Angular以哪种方式解析重复的指令/组件选择器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我包括两个模块,它们都使用相同的选择器定义了一个组件或指令(让我们说 a [routerLink] ).

If I include two modules which both define a component or directive with the same selector (lets say a[routerLink]).

ModuleA使用选择器 a [routerLink]

ModuleA defines directive with selector a[routerLink]

ModuleB使用选择器 a [routerLink]

ModuleB defines directive with selector a[routerLink]

AppModule包括两个模块,并在其模板中使用< routerLink ="..."></a> .

AppModule includes both modules and uses <a routerLink="..."></a> in its template.

然后使用哪个指令?Angular如何解决在运行时选择哪个组件/指令?行为记录在某处吗?

Which directive is used then? How does Angular resolve which component/directive is chosen at runtime? Is the behaviour documented somewhere?

推荐答案

如果组件在两个单独的模块中声明了具有相同选择器的组件,则Angular模板解析器将在整个应用程序树中查找最接近的组件.在这里,您可以看到工作示例(只需单击链接即可渲染在 TestModule 中声明的第二个"hello"组件: StackBlitz-具有相同选择器的两个组件的示例

In case of components with the same selector declared in two separate modules Angular template parser will look for the closest component in whole application tree. Here you can see working example (just click on the link that'll render second 'hello' component declared inside TestModule: StackBlitz - example of two components with same selector

这篇关于Angular以哪种方式解析重复的指令/组件选择器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆