Templatizer - 如何渲染多次相同的模板,Polymer 2.x
Templatizer - How to render multiple times same template, Polymer 2.x
在 Polymer 1.x 中,我习惯于编写这样的模板化代码:
renderTemplate(query, properties) {
let element = this.shadowRoot.querySelector(query);
this.templatize(element);
var instance = this.stamp(properties);
return instance;
}
效果很好。但是在 Polymer 2.x 中有一条新的错误消息 A <template> can only be templatized once
。好吧,这没有意义,因为我有 1 个模板,我想使用不同的属性多次重新分发它。
我在这里举例说明我的代码如何
我有 #template1
和 #template2
我想渲染 #template1
然后 #template2
然后 #template1
。
我渲染模板的步骤:
1) 模板化 #template1
2) 邮票属性
3) 模板化 #template2
4) 邮票属性
5 a) 模板化 #template1
=> 错误
5 b) 跳过模板化和标记属性 => #template2
呈现....
我怎样才能做到这一点?在渲染 #template2
之后调用 stamp()
将导致另一个 #template2
渲染。我想要 #template1
,但我不能 templatize #template1
,因为它已经被模板化了。并且 stamp
总是 "binded" 到最后一个模板化元素。
我做错了什么吗?我真的很讨厌 Polymer,因为它的文档不好,而且很难 google 有用的东西
我找到了一个可能不是最佳解决方案但有效的解决方法。我试图在源代码中搜索一些解决方案,但除了名为 __templatizeOwner
的 属性 之外没有任何有用的东西。此 属性 设置为所有模板化元素。从元素中删除此 属性 是方法。
renderTemplate(query, properties) {
let element = this.shadowRoot.querySelector(query);
if(element.__templatizeOwner) {
element.__templatizeOwner = null;
}
this.templatize(element);
var instance = this.stamp(properties);
return instance;
}
我不确定这可能有什么副作用(更多的内存使用或其他),但这是我能够找到的唯一方法。
在 Polymer 1.x 中,我习惯于编写这样的模板化代码:
renderTemplate(query, properties) {
let element = this.shadowRoot.querySelector(query);
this.templatize(element);
var instance = this.stamp(properties);
return instance;
}
效果很好。但是在 Polymer 2.x 中有一条新的错误消息 A <template> can only be templatized once
。好吧,这没有意义,因为我有 1 个模板,我想使用不同的属性多次重新分发它。
我在这里举例说明我的代码如何
我有 #template1
和 #template2
我想渲染 #template1
然后 #template2
然后 #template1
。
我渲染模板的步骤:
1) 模板化 #template1
2) 邮票属性
3) 模板化 #template2
4) 邮票属性
5 a) 模板化 #template1
=> 错误
5 b) 跳过模板化和标记属性 => #template2
呈现....
我怎样才能做到这一点?在渲染 #template2
之后调用 stamp()
将导致另一个 #template2
渲染。我想要 #template1
,但我不能 templatize #template1
,因为它已经被模板化了。并且 stamp
总是 "binded" 到最后一个模板化元素。
我做错了什么吗?我真的很讨厌 Polymer,因为它的文档不好,而且很难 google 有用的东西
我找到了一个可能不是最佳解决方案但有效的解决方法。我试图在源代码中搜索一些解决方案,但除了名为 __templatizeOwner
的 属性 之外没有任何有用的东西。此 属性 设置为所有模板化元素。从元素中删除此 属性 是方法。
renderTemplate(query, properties) {
let element = this.shadowRoot.querySelector(query);
if(element.__templatizeOwner) {
element.__templatizeOwner = null;
}
this.templatize(element);
var instance = this.stamp(properties);
return instance;
}
我不确定这可能有什么副作用(更多的内存使用或其他),但这是我能够找到的唯一方法。