简体   繁体   English

构造Angular2组件库的最佳方法是什么?

[英]What is the best way to structure an Angular2 component library?

I am looking to work on an Angular2 datepicker component as if for release and inclusion in multiple projects. 我正在寻找一个Angular2 datepicker组件,好像要发布并包含在多个项目中一样。 What is the best way to structure the project for this compared to a regular Angular2 project built with angular-cli ? 与使用angular-cli构建的常规Angular2项目相比,构造项目的最佳方法是什么? Are there any examples of good starter projects/seeds for such a task? 有没有很好的入门项目/种子可以完成此类任务的示例? Or should the component library actually be an angular2 application itself? 还是组件库实际上应该是angular2应用程序本身?

My initial assumption was that I could just create a standard project with angular-cli which has a single module (eg MyDatepickerModule ) which contains a hierarchy of components forming the datepicker however I don't know if this is the best way as I don't need everything that a full application provides. 我最初的假设是,我可以使用angular-cli创建一个标准项目,该项目具有单个模块(例如MyDatepickerModule ),其中包含构成日期选择器的组件层次结构,但是我不知道这是否是最好的方法,因为我没有不需要完整的应用程序提供的所有内容。

Thanks for any guidance! 感谢您的指导!

I would publish the library with AoT compatibility in mind. 我会考虑AoT兼容性发布该库。

This means compiling the source using the ngc compiler. 这意味着使用ngc编译器编译源代码。 In the distribution package I would publish the JS source, original html/css files, d.ts typings files and the ngc generated metadata.json files. 在分发包中,我将发布JS源,原始html / css文件,d.ts键入文件以及ngc生成的metas.json文件。

I recommend publishing the JS source with es2015 modules since this will make your library tree shakable. 我建议使用es2015模块发布JS源代码,因为这会使您的库树可摇动。 I would target es5 JS, but with es2015 modules . 我的目标是es5 JS,但使用es2015模块。 TypeScript allows for this hybrid mode by setting module to ES2015 and target to es5 in tsconfig.json. TypeScript通过在tsconfig.json中将模块设置为ES2015并将目标设置为es5来实现此混合模式。

Publishing these files will make your library AoT compatible and Tree shakable. 发布这些文件将使您的库AoT兼容并且可摇树。

This is all the consuming application needs in order to AoT compile your library into their complete application. 这就是所有需要使用的应用程序,以便AoT将您的库编译为完整的应用程序。

It's not recommended to publish TypeScript in your package since this would require the consumer to replicate your build environment (typings + TS compiler version). 不建议在您的程序包中发布TypeScript,因为这将要求使用者复制您的构建环境(类型+ TS编译器版本)。

You can also publish a JiT compatible umd bundle with inlined templates and css. 您还可以发布具有内联模板和CSS的与JiT兼容的umd软件包。 This can be helpful since it might not be practical do AoT during development since compilation is a bit slow. 这可能会有所帮助,因为在开发过程中进行AoT可能不切实际,因为编译有点慢。 The umd bundle will make it possible to use your library in a JiT based dev environment. umd软件包将使您可以在基于JiT的开发环境中使用您的库。 For production though you should definitely use the AoT version. 对于生产,尽管您绝对应该使用AoT版本。

The CLI is not ideal for publishing libraries since CLI is primarily a tool for building complete applications. CLI不是发布库的理想选择,因为CLI主要是用于构建完整应用程序的工具。 They might support libraries better in the future though. 他们将来可能会更好地支持图书馆。

Check out https://github.com/angular/material2 . 查看https://github.com/angular/material2 A work in progress, it's a library of controls and themes for Angular2 applying Material Design and is an excellent source for learning to build your own control library. 正在进行中的工作,它是Angular2应用Material Design的控件和主题的库,是学习构建自己的控件库的绝佳资源。

暂无
暂无

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

相关问题 在Angular2中验证反应形式字段的最佳方法是什么? - What is the best way to validate reactive form field in Angular2? 为 React 和 Preact 创建组件库的最佳方法是什么? - What is the best way to create component library for both React and Preact? 在 Angular2 中无需用户操作即可在获取数据后更新组件的最佳方法 - The best way to update component after fetch data without user action in Angular2 在 Angular 6 中重新加载组件的最佳方法 - Best way to reload component in Angular 6 Angular2 - 组件变量/组件类属性的双向数据绑定? - Angular2 - two way databinding on a component variable / component class property? 在组件渲染视图后更新dom,这是Angular2的最佳实践吗? - Update dom after component rendered the view, best practice in Angular2? Angular2 - 具有单独模板/样式文件的库组件 - Angular2 - Library component with separate template/style files 在Angular(PhoneGap)中,构造AJAX调用并将返回值用作可存储JSON数据的最佳方法是什么? - In Angular (PhoneGap), what's the best way to structure an AJAX call and use the return as storable JSON data? 构造Angular应用程序以便控制器可以具有$ state.go(..)的最佳方法是什么? - What is the best way to structure Angular application so controllers can have $state.go(..)? 为 React 构建配置文件的最佳方法是什么? - What is the best way to structure a configuration file for React?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM