V
V
Vladimir2019-03-07 18:42:02
Vue.js
Vladimir, 2019-03-07 18:42:02

How to correctly use class instances as data parameter values ​​in vue.js?

There is a class:

class User {
    constructor(firstName, lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
    }

    get fullName() {
        return this.firstName + ' ' + this.lastName;
    }

    getFullName() {
        return this.firstName + ' ' + this.lastName;
    }
}

If i use it in vue.js component like this
export default {
    data: () => ({
        user: new User('Иван', 'Иванов')
    })
}

, then vue.js will redefine the instance of the User class into a regular object, i.e. throw out getters, class setters, throw out class functions and throw out its getters and setters to ensure reactivity. And I would like to keep the ability to receive fullName. It would be absolutely ideal if the fullName getter also became reactive.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
0
0xD34F, 2019-03-07
@z3d01n

vue.js will override the instance of the User class into a regular object

OH REALLY??!

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question