Put a default value in the binding.value Vue Directive

1

I've been creating a simple directive and wanted to know how to put a value if the binding.value is undefined in a simpler way, much like the Vue Component where defalut value can be?

Example:

Vue.directive('color', {
    bind: function (el, binding) {
        var prop = binding.value,
            color = prop.color

            if(color == undefined) color = '#FFFFFF'

        el.style.backgroundColor = color
    }
})
    
asked by anonymous 07.09.2017 / 21:19

1 answer

1

In a more objective and direct way you can solve this:

Vue.directive('color', {
    bind: function (el, binding) {
        var prop = (binding.value || { color: '#FFFFFF' }),
            color = prop.color

        // Não vai mais precisa dessa checagem.
        //if(color == undefined) color = '#FFFFFF'

        el.style.backgroundColor = color
    }
})
    
11.09.2017 / 17:21