niko craft
niko craft

Reputation: 2977

watching one value, computing the other one, how to update computed value when watched changes

I am watching one value and I computed another value.

Something like this:

    computed: {
        modeText: function () {
            if(this.mode == 'create')
                return 'Create'
            else
                return 'Edit'
        }
    },
    watch: {
        mode(val, old) {
            if(val == 'create')
                update modeText
            else
                update modeText
        },
    },

How do I recompute the computed value when watched value updates?

Upvotes: 0

Views: 324

Answers (1)

kevguy
kevguy

Reputation: 4438

It seems like modeText is only dependent on mode, so instead of using computed, you can go for something simpler:

data: {
    modeText: '',
    mode: ''
},
watch: {
    mode(val, old) {
        this.modeText = this.updateModeText(val);
    },
},
methods: {
    updateModeText(val) {
        if (val === 'create') {
            return 'Create';
        }
        return 'Edit';
    }
}

Upvotes: 1

Related Questions