0

How can i access to data values inside itself in vue.js?

Code:

data: function(){
    return {
        foo: 123,
        bar: this.foo
    }
}

In this case I got undefined

1 Answer 1

2

There are several options I see:

data: function(){
    const data = {
        foo: 123
    };

    data.bar = data.foo;

    return data;
}

or

data: function(){
    const data = {
        foo: 123
    };

    return {
        ...data,
        bar: data.foo
    };
}

You've got undefined because of this.foo refers to function(){`s context, not object`s context.

Sign up to request clarification or add additional context in comments.

Comments

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.