我正在尝试 Octane,出于某种原因,如果我在模板中显示一个数组并向其中添加一个新对象,则 UI 不会更新。我究竟做错了什么?
这是我的模板:
<label for="new-field-name">Field Name</label>
<Input id="new-field-name" @value={{this.newFieldName}} type="text" />
<button {{on "click" this.addField}}>Add field</button>
{{#each this.fields as |field|}}
<p>{{field.name}}</p>
{{/each}}
和组件:
import Component from '@glimmer/component';
import { tracked } from '@glimmer/tracking';
import { action } from '@ember/object';
export default class ConfigControlsComponent extends Component {
@tracked fields = []
@tracked newFieldName = ''
@action addField() {
this.fields.push({
name: this.newFieldName
})
console.log(this.fields)
}
}
显示console.log
了添加了新对象的数组,并fields
跟踪了数组,但是当我单击按钮时没有任何变化。