QBM5
QBM5

Reputation: 2788

Vue.js custom directive accessing context of a loop

Vue.directive("custom", {
    inserted(el, binding, vnode) {
        let val = binding.value;
        let arg = binding.arg
        let mods = binding.modifiers
        let expr = binding.expression
        let cont = vnode.context
        .... // modify loop
    }
})

I am attempting to build a custom directive in vue. this directive can be fire inside or outside of a v-for loop. When it is fired inside the v-for loop I need the context of the current iteration

v-for="item in list"  // i need the context for item

But I am at a loss of how to get it. even if used the version of v-for that included the index

v-for="(item, index) in list"  // i need the context for item

neither of these values are accessible in the the directive insert function. At least I cannot find them anywhere.

Any help locating these values would be greatly appreciated

Upvotes: 2

Views: 943

Answers (2)

Ricky Ruiz
Ricky Ruiz

Reputation: 26791

Use a value in the binding directive hook argument.

For example in v-custom="item", the value would be the item.

Vue.directive("custom", {
    inserted(el, binding, vnode) {
        let val = binding.value; // this value is the item
        let arg = binding.arg
        let mods = binding.modifiers
        let expr = binding.expression
        let cont = vnode.context
        .... // modify loop
    }
})

Example (see log):

Vue.directive("custom", {
  inserted(el, binding, vnode) {
    let val = binding.value;
    let arg = binding.arg
    let mods = binding.modifiers
    let expr = binding.expression
    let cont = vnode.context

    console.log(val)
  }
})

new Vue({
  el: "#app",
  data: {
    todos: [{
        text: "Learn JavaScript",
        done: false
      },
      {
        text: "Learn Vue",
        done: false
      },
      {
        text: "Play around in JSFiddle",
        done: true
      },
      {
        text: "Build something awesome",
        done: true
      }
    ]
  },
  methods: {
    toggle: function(todo) {
      todo.done = !todo.done
    }
  }
})
body {
  background: #20262E;
  padding: 20px;
  font-family: Helvetica;
}

#app {
  background: #fff;
  border-radius: 4px;
  padding: 20px;
  transition: all 0.2s;
}

li {
  margin: 8px 0;
}

h2 {
  font-weight: bold;
  margin-bottom: 15px;
}

del {
  color: rgba(0, 0, 0, 0.3);
}
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/vue.js"></script>

<div id="app">
  <h2>Todos:</h2>
  <ol>
    <li v-for="todo in todos" v-custom="todo">
      <label>
        <input type="checkbox"
          v-on:change="toggle(todo)"
          v-bind:checked="todo.done">

        <del v-if="todo.done">
          {{ todo.text }}
        </del>
        <span v-else>
          {{ todo.text }}
        </span>
      </label>
    </li>
  </ol>
</div>

Upvotes: 1

artoju
artoju

Reputation: 1712

From https://v2.vuejs.org/v2/guide/custom-directive.html

Directive hooks are passed these arguments:

el: The element the directive is bound to. This can be used to binding: An object containing the following properties.
...
value: The value passed to the directive. For example in v-my-directive="1 + 1", the value would be 2.

And you have it here already

let val = binding.value;

You just need to pass it to the directive.

<div v-for="item in list" :v-custom="item"></div>

Upvotes: 0

Related Questions