I have some JSON objects passed from an API server like: {"id": 4, "name": "foo", "bar": {"key1": "value1", "key2": "value2"}}
I want to wrap this object with a Javascript class so I can handle the JSON change easily (It's under development and change from time to time). It's a very basic OOP: Wrap the data, then use setters and getters:
export default class Issue {
constructor(json) {
this.data = json
}
getId() {
return this.data.id
}
isOpen() {
return this.data.status.id !== 6
}
...
}
But in Vue.js I don't know how to do this while keeping it reactively. For example, in a component I have
data: () => {
return {
issues: []
}
}
Then for (const json of jsons) { this.issues.push(json) } will be reactive, but for (const json of jsons) { this.issues.push(new Issue(json)) } will be not, right?
I think my ideal is that I can use Component as a data class, but I don't know how to new a component, and is it effective to use Components as data classes.
What should I do?
.push()instead of.append()? Yourissuesarray will be reactive provided you use appropriatekeyprops, egv-for="issue in issues" :key="issue.getId()". If the data inside eachIssueis meant to change though, then I don't think that would workclassesare more popular in the Angular world - in Vue world dataobjectsseem to be the preferred way..push(). Was writing python. Maybe I can do with class-like objects.