When you use tracked
with arrays, you need to "reset" the array so that Ember notices that there has been a change. Try doing this.fields = this.fields
after pushing a new object into the array.
Edit: some linters will guard against self-assignment. So, instead, we could pull from immutability patterns, and set using a new array, as shown below.
export default class ConfigControlsComponent extends Component {
@tracked fields = []
@tracked newFieldName = ''
@action addField() {
// add this line
this.fields = [...this.fields, {
name: this.newFieldName
}];
}
}
If you are trying to use tracked
with an object instead of an array, you have two options:
First, you could create a class where all the properties on the object are tracked:
import { tracked } from '@glimmer/tracking';
class Address {
@tracked street;
@tracked city;
}
class Person {
address = new Address();
get fullAddress() {
let { street, city } = this.address;
return `${street}, ${city}`;
}
}
Or, second, you could use the same "reset" approach as the array example above.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…