4
I’m starting now with Vue 2.0 and I’m creating directives so that I can manipulate the style of an element based on the content of that directive, so:
Vue.directive('margin', function (el, binding) {
$(el).css('margin', binding.value);
});
My problem is that in order for me to give a Binding no value, I am obliged to leave the field always formatted as string, and this I see as unnecessary... so:
<div class="foo" v-margin="'auto'"></div>
If by chance I take the simple quotes from inside the Binding, occurs the error message:
[Vue warn]: Property or method "auto" is not defined on the instance but
referenced during render. Make sure to declare reactive data properties
in the data option.
Is there any way to "force" the directive to always interpret my Binding as plain text? so I wouldn’t need to pass simple quotes from the 'auto'
.
NOTE I have tried to define within the directive a
toString()
in the Binding parameter but without success, thus:
Vue.directive('margin', function (el, binding) {
$(el).css('margin', binding.value.toString()); // <- toString();
});
And if you try
String(binding.value)
?– Miguel
String(Binding.value) attempts to convert a variable to the String type which in this case is the same as Binding.value.toString().
– LeandroLuk
Because I saw this, http://stackoverflow.com/questions/3945202/whats-the-difference-between-stringvalue-vs-value-tostring I thought it might work
– Miguel
It is a good logic to use String, but Vue interprets a null.toString() as "";D
– LeandroLuk
I even find that odd, http://stackoverflow.com/documentation/vue.js/2368/custom-directives#t=201701091450173776812 and here https://vuejs.org/v2/guide/custom-directive.html seems to work just fine
– Miguel