有谁能告诉我聚合物,x-tag和vanilla js之间的区别吗?
我在我的项目中使用了聚合物,但我想比较聚合物,x-tag和香草js。
发布于 2014-07-01 17:15:39
VanillaJS只意味着在纯JS中使用web组件,而不使用任何框架/包装器。
您必须注册您的自定义元素,自己打印出该元素并处理数据绑定。
x-tag和Polymer都为web组件提供了方便且自以为是的包装器,大大减少了样板代码。
Polymer库提供了最具解密能力的方法(关于数据绑定、定义模板等)
这是使用x-tag时的样子:
xtag.register('x-accordion', {
// extend existing elements
extends: 'div',
lifecycle:{
created: function(){
// fired once at the time a component
// is initially created or parsed
},
inserted: function(){
// fired each time a component
// is inserted into the DOM
},
removed: function(){
// fired each time an element
// is removed from DOM
},
attributeChanged: function(){
// fired when attributes are set
}
},
events: {
'click:delegate(x-toggler)': function(){
// activate a clicked toggler
}
},
accessors: {
'togglers': {
get: function(){
// return all toggler children
},
set: function(value){
// set the toggler children
}
}
},
methods: {
nextToggler: function(){
// activate the next toggler
},
previousToggler: function(){
// activate the previous toggler
}
}
});这是使用聚合物时的样子:
<polymer-element name="polymer-accordion" extends="div" on-click="{{toggle}}">
<template>
<!-- shadow DOM here -->
</template>
<script>
Polymer('polymer-accordion' {
created: function() { ... },
ready: function() { ... },
attached: function () { ... },
domReady: function() { ... },
detached: function() { ... },
attributeChanged: function(attrName, oldVal, newVal) {
},
toggle : function() {....},
get togglers() {},
set togglers(value) {},
nextToggler : function() {},
previousToggler : function() {},
});
</script>
</polymer-element>发布于 2014-08-04 17:56:11
Web组件是浏览器中的本机实现。聚合物是一个库,它在Web组件技术之上充当一个非常薄的层。X-Tag是另一个更薄的库,因为它只依赖于四种Web组件技术中的一种。
我写了一篇关于这方面的文章:http://pascalprecht.github.io/2014/07/21/polymer-vs-x-tag-here-is-the-difference/
https://stackoverflow.com/questions/24487144
复制相似问题