简体   繁体   English

将全局辅助函数添加到 bit.dev 项目

[英]Add global helper functions to bit.dev project

I am pretty happy with bit.dev .我对bit.dev很满意。 But there is one point I am not getting to work.但是有一点我没有开始工作。 I need some global functions like formatting currency or saving same constants and enums, that I want to use from different components.我需要一些全局函数,例如格式化货币或保存相同的常量和枚举,我想从不同的组件中使用它们。

One way would be to add the code to every single component, but that would be just code duplication.一种方法是将代码添加到每个组件中,但这只是代码重复。 The other way could be to create a component with just constants or helper functions, but I don't know whether bit is designed for that way.另一种方法可能是创建一个只有常量或辅助函数的组件,但我不知道 bit 是否是为这种方式设计的。

Is there some best practice how I could solve that problem?是否有一些最佳实践可以解决这个问题?

best regarts!最好的问候!

I work at Bit.我在比特工作。 In Bit, we solved it by having a scope for utilities functions, which we named toolbox .在 Bit 中,我们通过使用 scope 来解决这个问题,我们将其命名为toolbox

Inside that scope, you can publish each of your functions as a nodeJS component.在 scope 内部,您可以将每个函数发布为 nodeJS 组件。 You can give it a look here to our Toolbox您可以在这里查看我们的工具箱

Inside of it, we created several namespaces.在其中,我们创建了几个命名空间。 We have one for strings, arrays, filesystem, and many others.我们有一个用于字符串、arrays、文件系统等等。

The principle is the same as you have been working so far: create your component, export it, and import it to any project you may need.原理与您迄今为止的工作相同:创建您的组件,将其导出,然后将其导入到您可能需要的任何项目中。

Let me know if that helped让我知道这是否有帮助

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

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