Richard H Richard H - 1 year ago 216
Javascript Question

Vue.js: How to read $index inside computed function?

I have list of links:

<ul id="menu">
<li v-for="item in items">
<a href v-bind:href=link>{{item.message}}</a>

var example1 = new Vue({
el: '#menu',
data: {
items: [
{ message: 'Link1' },
{ message: 'Link2' }
computed: {
link: function() {
return f($index) // how do I access current array index ??

I can achieve the desired result by using mustache markup in a
attribute but it must be possible to access this
variable from within a computed function?

Answer Source

You can't.

This is what methods are for:

<a href v-bind:href="link($index)">{{item.message}}</a>

methods: { 
  link: function(index) {
    return f(index)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download