私は「Quick Tour of Polymer」の後に、配列に基づいて要素を繰り返す方法を説明するセクションがありますが、それはテンプレートリピータで行う方法を示しています本当に後ろからその仕事が分かっている。私は自分のリピータをやろうとしましたが、Polymerは自分のコードをエスケープ文字のような文字列として注入します。関数からHTMLを注入する
コード:
<dom-module id="employee-list">
<template>
[[employe()]]
</template>
<script>
class EmployeeList extends Polymer.Element {
static get is() {
return 'employee-list'
}
constructor() {
super()
this.employees = [
{first: 'Bob', last: 'Li'},
{first: 'Ayesha', last: 'Johnson'},
{first: 'Fatma', last: 'Kumari'},
{first: 'Tony', last: 'Morelli'}
]
}
employe(employees = this.employees) {
let template = '<div>Employee List</div>'
template += employees.map((currentEmployee, id) => {
return `<div>Employee ${id}, FullName : ${currentEmployee.first + ' ' + currentEmployee.last}</div>`
})
return template
}
}
customElements.define(EmployeeList.is,EmployeeList)
</script>
</dom-module>
結果:
<div>Employee List</div><div>Employee 0, FullName : Bob Li</div>,<div>Employee 1, FullName : Ayesha Johnson</div>,<div>Employee 2, FullName : Fatma Kumari</div>,<div>Employee 3, FullName : Tony Morelli</div>
そして私はあなたが使用できる2
あなたは'使用しない理由はありますか? –