vue.js - Vue2: Custom Directive Like v-if -


i'm trying create custom directive v-if render if data being passed element isn't empty. example:

<div v-if="!_.isempty(data.employer)">{{ data.employer.name }}</div> 

this render if data.employer isn't empty won't throw reference error. i'm trying create directive simplify v-notempty="data.employer" , run logic inside directive issue it's doing hook on custom directive after element being rendered throws reference error employer undefined.

is there way custom directive work v-if runs logic before element created. had far:

    vue.directive('notempty', (el, binding) => {   if (_.isempty(binding.value)) {     el.style.display =  'none';   } else {     el.style.display =  'initial';   } }); 

you need write directive code inside bind hook function. (reference)

bind:

called once, when directive first bound element. can one-time setup work.

so code should be:

vue.directive('notempty', {   bind: function (el, binding, vnode) {     if (_.isempty(binding.value)) {       el.style.display =  'none';     } else {       el.style.display =  'initial';     }   } }); 

Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -