简体   繁体   English

Mixins作为Polymer 2.0中的实用程序库

[英]Mixins as utility library in Polymer 2.0

I am working in web application project which is made in Polymer 2.0, All Custom elements extends some Mixins. 我正在使用Polymer 2.0制作的Web应用程序项目,所有Custom元素都扩展了一些Mixins。 Some of those Mixins just provide utility functions to custom elements just like Date Time Utility functions or any Math related functions. 其中一些Mixins只是为自定义元素提供实用程序功能,就像Date Time Utility函数或任何与Math相关的函数一样。 My question is whether to use mixins & extends them to custom elements or just wrap them in plain java-script file and load that java-script file on index.html or entry point of application and use as global scope just like we use lodashjs or underscore.js. 我的问题是是否使用mixins并将它们扩展到自定义元素或者只是将它们包装在普通的java脚本文件中,并在index.html或应用程序的入口点加载该java脚本文件,并像我们使用lodashjs一样用作全局范围underscore.js。

The problem i find with Mixins is it always get applied to prototype chain of each custom element class object, So i end up those same utility methods with each custom element of my application. 我用Mixins找到的问题是它总是应用于每个自定义元素类对象的原型链,所以我最终使用我的应用程序的每个自定义元素的那些相同的实用程序方法。

Please suggest me best approach for Utilities in Polymer related apps. 请建议我在Polymer相关应用程序中使用Utilities的最佳方法。

That is a rather tough question to answer properly... as the real answer is just "both are valid solutions - it depends on your use case". 这是一个相当棘手的问题,无法正确回答......因为真正的答案只是“两者都是有效的解决方案 - 这取决于你的用例”。 Which I guess is not helping too much. 我猜这并没有太大帮助。 So let's give you some context. 那么让我们给你一些背景信息。

For real utilities, it is probably best to put them in a class as a static function. 对于实用的实用程序,最好将它们作为静态函数放在类中。

 class MathHelper { static addOne(value) { return value + 1; } } class ElOne extends HTMLElement { connectedCallback() { this.innerHTML = `5 + 1 = ${MathHelper.addOne(5)}`; } } customElements.define('el-one', ElOne); 
 <el-one></el-one> 

Mixins, on the other hand, can serve a similar need but should be more "connected" to the Element they are used on. 另一方面,Mixins可以满足类似的需求,但应该更多地“连接”它们所使用的元素。 (Sorry for the terrible example but from the top of my head I could not come up with something better) (对不起这个可怕的例子,但从头顶我无法想出更好的东西)

 const MathMixin = superclass => class extends superclass { addOneTo(prop) { this[prop] += 1; } } class ElTwo extends MathMixin(HTMLElement) { constructor() { super(); this.number = 5; } connectedCallback() { this.addOneTo('number'); this.innerHTML = `5 + 1 = ${this.number}`; } } customElements.define('el-two', ElTwo); 
 <el-two></el-two> 

Conclusion: 结论:

Class: 类:

  • if possible use it 如果可能的话使用它
  • maybe even use static functions 甚至可能使用静态函数
  • it's easier to test and to maintain 它更容易测试和维护

Mixin: 混入:

  • if you need to enhance another class eg to allow myElemenet.changeLanguage('de') , ... 如果你需要增强另一个类,例如允许myElemenet.changeLanguage('de') ,...
  • if you need any other state of the class 如果你需要任何其他阶段的状态

I hope this clears it up at least a bit. 我希望这至少可以清除它。 If you have more concrete examples what needs to be implemented we could propose a recommended way of implementation. 如果您有更具体的例子需要实施,我们可以提出一种推荐的实施方式。

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

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