It’s helpful to think of the data option as the schema for your component state. Note that vm.$get and vm.$set both accept paths.ĭespite the existence of these methods, make sure to only add observed fields when necessary. Vue component instances also have corresponding instance methods: Observed Arrays are augmented with two methods: Again, you can use augmented methods to notify Vue.js about those changes. arr = value), Vue will not be able to pick up the change. The difference between $add and $set is that $add will return early if the key already exists on the object, so just calling obj.$add(key) won’t overwrite the existing value with undefined.Ī related note is that when you change a data-bound Array directly by setting indices (e.g. These methods can be used to add / delete properties from observed objects while triggering the desired DOM updates. To deal with this constraint, observed Objects are augmented with three methods: However, in ECMAScript 5 there is no way to detect when a new property is added to an Object, or when a property is deleted from an Object. Adding and Deleting PropertiesĪs mentioned above, Vue observes data by converting properties with fineProperty. It is ok to initialize a field as an empty object, and set it to a new object with nested structures later, because Vue will be able to walk the nested properties of this new object and observe them. You don’t have to set every single nested property in your data though. If a property is not present when the instance is created, Vue will not be able to track it. The reason for this is that Vue observes data changes by recursively walking the data object and converting existing properties into reactive getters and setters using fineProperty. It’s recommended to initialize your data like this instead of an empty object: 1 For example, given the following template: 1 It is recommended to initialize all the data properties that needs to be reactive upfront in the data option. Vue’s data observation model favors deterministic data models.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |