为了单独测试我的一些聚合物自定义元素,我希望能够传入 js 对象文字来获取通常来自父元素的一些属性。我很难弄清楚如何做到这一点。请参阅此示例代码。如果它按照我希望的方式工作,它会显示一个 1 和一个 2 彼此相邻,但它不起作用。
<script src="http://www.polymer-project.org/webcomponents.js"></script>
<link rel="import" href="http://www.polymer-project.org/components/polymer/polymer.html">
<polymer-element name="my-element" attributes="stuff">
<template>
{{stuff.one}} {{stuff.two}}
</template>
<script>
Polymer('my-element', {
ready: function () {
console.log(this.stuff);
}
});
</script>
</polymer-element>
<my-element stuff='{"one": 1, "two": 2}'></my-element>
Polymer 仅将 JSON 文本转换为对象,如果您初始化stuff
具有空哈希的属性:
Polymer('my-element', {
stuff: {},
ready: function () {
console.log(this.stuff);
}
});
如果没有这个,stuff
属性作为字符串传入。数组也是如此。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)