Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add vue directive on condition

Tags:

vue.js

is it possible to set a directive on a condition?

I have a "sticky" directive to make elements sticky on the screen. And I have a social share component where I am using it

<tempalte>
    <div class="social-share" v-sticky>
        ....
    </div>
</template>

However, now I need a condition. I would love making this over a prop.

<social-share :is-sticky="true">

Is there an easy way to add the directive?

I've tried to bind it with v-bind / :

<div class="social-share" :v-sticky="isSticky">

But this will end up, with rendering "v-sticky" into the tag.

like image 604
Jakub Juszczak Avatar asked May 30 '16 13:05

Jakub Juszczak


People also ask

How do you use if condition in Vue?

The v-if directive is a Vue. js directive used to toggle the display CSS property of a element with a condition. If the condition is true it will make it visible else it will make it invisible.

Does Vue directive?

Vue. js comes pre-packaged with a number of directives that developers commonly use when working within the framework. Directives such as v-if , v-show , v-on , v-model , v-bind , v-html , and more are all provided for you out-of-the-box. All of these provided directives start with v- .


3 Answers

You may consider defaulting to true when no value is passed to the directive.

bind(el, binding) {
    if ((! binding.hasOwnProperty('value')) || binding.value) {
        // The user wants the directive to be applied...
    }
}

In your template:

<template>
    <social-share v-sticky> <!-- Will apply the directive -->
    <social-share v-sticky="true"> <!-- Will apply the directive -->
    <social-share v-sticky="false"> <!-- Will NOT apply the directive -->
</template>
like image 141
Kamal Khan Avatar answered Oct 06 '22 23:10

Kamal Khan


Please keep in mind, that the original question was asked 2016! Thus this is a vue 1.x solution

ok got it working over directive params. And added the condition in the directive itself.

Update

Because requested, here is some example code: The SocialShare.vue is the parent component which has the sticky directive.

As you see, the div with the sticky directive receive a prop sticky which is defined inside the directive.

// SocialShare.vue
<template>
  <div class="social-share" v-sticky :sticky="true">
    <h5>{{ headline }}</h5>
    // your code here.
  </div>
</template>

<script>
import stickyDirective  from '../../directives/sticky';

  export default {
    directives: {
      'sticky': stickyDirective,
    }
}
</script>

Ok now the directive. You can add params to directives itself. Thats why sticky works on a div element.

You simply declare your props in a params array and can then access it over this.params

// sticky directive

const sticky =  {
   params: [
    'sticky'
  ],
  bind() { 
    if (this.params.sticky) {
      console.log('Set to true')
    }
  }

  unbind() {

  },
}

export default sticky
like image 40
Jakub Juszczak Avatar answered Oct 07 '22 00:10

Jakub Juszczak


I stumbled upon this, however the solution was for V1 and since then they have removed the params from the v-directive. The binding has many values in alternative tho

https://v2.vuejs.org/v2/guide/custom-directive.html#Directive-Hook-Arguments

An alternative solution

// SocialShare.vue
<template>
  <div class="social-share" v-sticky="true">
    <h5>{{ headline }}</h5>
    // your code here.
  </div>
</template>

<script>
import stickyDirective  from '../../directives/sticky';

  export default {
    directives: {
      'sticky': stickyDirective,
    }
}
</script>

and

   // sticky directive

const sticky =  {
  bind(binding) { 
    if (binding.value) {
      console.log('Set to true')
    }
  }

  unbind() {

  },
}

export default sticky
like image 42
marclave Avatar answered Oct 07 '22 00:10

marclave