Custom project-level templates for angular components generated via angular-cli

做~自己de王妃 提交于 2019-12-10 17:35:54

问题


How do I add custom angular component schematics to existing project?

I would like to have e.g. my-page component templates that will replicate existing \node_modules\@schematics\angular\component\ files but with edited templates.

I expect it to be achievable through angular-cli e.g. ng g my-page pagename command with schematics attribute.

Currently I modified code for existing agular's schematics under \node_modules\ so this works fine for me (until next package installation) but I would like to share this with the team so everyone will use predefined templates that will be stored in e.g. src\team-schematics\.

How do I achieve this?

I tried to create new schematic via schematics schematic --name my-schematic, copied angular schematic into \ClientApp\src\app\my-schematic\\ and trying to launch $ ng generate my-page --collection \ClientApp\src\app\my-schematic but no luck as of now:

Error: Could not find module "\\ClientApp\\src\\app\\my-schematic\\" from "C:\\Users\\xxx\\Projects\\yyy


回答1:


You shouldn't modify core schematics. On the other hand, you can create your own schematic.

Creating the schematic is a step-by-step process. You can find the same at Schematics — An Introduction.




回答2:


As for as Angular CLI we can only have one Schematics. Might be you can create custom schmatics '@foo/schematics' and extends the @angular/schematics in angular.json and customize the schematics based on your needs.

ng new C myComponent --collection=@foo/schematics


来源:https://stackoverflow.com/questions/52254130/custom-project-level-templates-for-angular-components-generated-via-angular-cli

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