简体   繁体   English

Angular:如何在不编写新组件的情况下覆盖组件模板

[英]Angular: how to override a component template without writing a new component

I'm looking for a way to use my components replacing the standard templates with custom ones, but still keeping AOT compatibility. 我正在寻找一种使用组件替换自定义模板的标准模板的方法,但仍保持AOT兼容性。

I know it can be done writing a new component which replaces the original template, but I'd like to provide some convention-over-configuration instead. 我知道可以编写一个替换原始模板的新组件来完成此操作,但是我想提供一些配置约定。

Goal : if you use my-component and want to replace its original template, you just have to create a template named overrides/my-component.html in the folder hosting the parent component. 目标 :如果您使用my-component并想要替换其原始模板,则只需在承载父组件的文件夹中创建一个名为overrides/my-component.html的模板。

Take for instance the ad-banner component used in the Dynamic Component Loader official Angular example: in that example hero-job-ad and hero-profile components are simply meant at replacing the original template within a certain context. 例如,在Dynamic Component Loader官方Angular示例中使用的广告横幅组件:在该示例中hero-job-adhero-profile组件仅是为了在特定上下文中替换原始模板。 I consider that code as quasi-configuration, so I'd like to provide some convention over it. 我认为该代码为准配置,因此我想提供一些约定。

I thought this feature could be implemented adding some custom resource loader to webpack at compile time, but so far I cannot figure out how. 我以为可以在编译时向Webpack添加一些自定义资源加载器来实现此功能,但是到目前为止我还不知道如何实现。

There is no built-in solution - but since you want to have compile time override posibility, why than how about writing shell script that will recursively copy everything from overrides to corresponding directories in main sources. 没有内置的解决方案-但是既然您想拥有编译时覆盖的可能性,那为什么不写一个shell脚本来递归地将所有内容从覆盖复制到主源中的相应目录。

Another option would be to use dynami components https://blog.angularindepth.com/here-is-what-you-need-to-know-about-dynamic-components-in-angular-ac1e96167f9e 另一种选择是使用动态组件https://blog.angularindepth.com/here-is-what-you-need-to-know-about-dynamic-components-in-angular-ac1e96167f9e

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM