0

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?

3
  • Did you mean .push() instead of .append()? Your issues array will be reactive provided you use appropriate key props, eg v-for="issue in issues" :key="issue.getId()". If the data inside each Issue is meant to change though, then I don't think that would work Commented May 3, 2021 at 8:20
  • 1
    I think that data classes are more popular in the Angular world - in Vue world data objects seem to be the preferred way. Commented May 3, 2021 at 8:23
  • Oh yeah .push(). Was writing python. Maybe I can do with class-like objects. Commented May 3, 2021 at 9:49

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.