Accessing $index in Vue.js computed properties

Here is a list of links:

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


  var example1 = new Vue({
  el: '#menu',
  data: {
     items: [
        { message: 'Link1' },
        { message: 'Link2' }
     ]
  },
  computed: { 
      link: function() {
          return f($index)    // Is it possible to access the current array index here?
      }
  }
  })

To achieve the desired result, I need to use mustache markup in the href attribute. However, how can I access the $index variable from within a computed function?

Answer №1

It is not possible.

The proper way to achieve this is by using methods:

<span v-bind:id="generateId($index)">{{item.name}}</span>

methods: { 
  generateId: function(index) {
    return createUniqueId(index)
  }
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Integrating an API with a Discord bot using an embedded link in Discord.js

I am currently in the process of creating a bot that can generate and embed links to display manga titles, tags, and other information based on user-input digits. I have been exploring an API called this and I am eager to learn the most effective method ...

Input Error in ASP.NET JavaScript

Within my ASP.NET website, I have implemented an <asp:TextBox /> where the text input is encoded using HttpUtility.HtmlEncode(); Furthermore, the page is equipped with validation controls like <asp:RequiredFieldValidator /> and <asp:CustomV ...

What could be the reason for parent props not updating in child components in VueJS?

Hello, I am new to VueJS and encountering an issue. In the main.js file, I am passing the user variable to App.vue using props. Initially, its value is {} The getLoginStatus() method in main.js monitors the firebase authentication status, and when a user ...

Leveraging Selenium to extract text from a dynamically populated DIV using JavaScript

I am currently utilizing Selenium to automatically retrieve all comments from a New York Times article. Once the comments are loaded, my goal is to extract them and save the information for future use. However, upon inspecting the source code of the articl ...

Difficulties with choosing predecessors

Is there a way in the HTML snippet below to extract the checkall from the thing? .. <input type="checkbox" class="checkall"><label>Check all</label> <ul> <li><input type="checkbox" class="thing"><label>Thing 1 ...

Click the link to capture the ID and store it in a variable

Currently working on a project involving HTML and JavaScript. Two HTML pages ("people.html" and "profile.html") are being used along with one JavaScript file ("people.js") that contains an object with a list of names, each assigned a unique ID: var person ...

Clicking on a JavaScript Button with Selenium in Java

I have been diligently working on automating a large-scale project using Selenium in Java, and overall it has been a smooth process. However, I recently encountered a roadblock. My current challenge involves submitting WebElements for a multi-select menu ( ...

Visualizing Dynamic Path on VueJS Using Polygon Map

I am facing some issues with implementing Google Maps in Vue.js. I have created a polygon component as shown below: <script> export default { name: "MapPolygon", props: { google: { type: Object, ...

The positioning of CSS arrows using the "top" attribute is not relative to the top of the page when using absolute values

I am currently working on positioning the arrow in the screenshot using TypeScript calculations. However, I am facing an issue where the position is being determined based on the top of the black popup instead of the top of the screen. From the top of the ...

Jest fails to pass when encountering the double colon

Having issues testing a React app using Jest. I encounter errors when running my code: FAIL src\App.test.js ● Test suite failed to run C:/Users/user1/Projects/map-editor/src/App.js: Unexpected token (40:33) 38 | <div cla ...

Struggling to incorporate pagination with axios in my code

As a newcomer to the world of REACT, I am currently delving into the realm of implementing pagination in my React project using axios. The API that I am utilizing (swapi.dev) boasts a total of 87 characters. Upon submitting a GET request with , only 10 cha ...

An issue arises when attempting to write to the database specifically when the model is imported from a separate file

There seems to be an issue with saving the model to the database when it's imported from another file. The error message received is: MongooseError: Operation users.insertOne() buffering timed out after 10000ms at Timeout. (/var/www/bhp_2/server/nod ...

Ways to verify the click status of a button prior to initiating an AJAX request with jQuery?

I am facing an issue with a button that needs to be clicked by the user before submitting the form. Here's the code snippet for the button: $('#chooseButton') .on( 'click', function() { console.log("user ha ...

Modifying the $scope within a controller

I need to update the $scope within the controller based on the object that is being clicked. The current code looks like this: var blogApp = angular.module('blogApp', ['ngSanitize', 'ngRoute']); blogApp.controller('blog ...

actions with frontend routing for CRUD operations

Imagine you are creating a simple CRUD todo application. Whether you choose to use Angular, React, or Vue for routing, the setup will be similar: /todos => see all todos /todos/:id => view one todo by id /todos/:id/edit => edit one todo by id /t ...

Creating a Doctrine query output in the form of a basic JSON array

Within my database, there exists an entity called Graphique. It is represented by the following schema: class Graphique { /** * @var integer * * @ORM\Column(name="id", type="integer", nullable=false) * @ORM\Id * @ORM ...

The collection contains a pair of elements, and the result of calling array.length is

My code is currently dealing with an array of BufferedImage objects. When I checked the length property, it returned a value of 20 even though the array only contained two elements at the breakpoint. How could this discrepancy occur? ...

Modify the chosen dates in the date range picker tool designed for Twitter Bootstrap

I recently started using the date range picker for Twitter Bootstrap, a creation by Dan Grossman, which you can find here. Upon initialization, I realized that setting pre-defined values like startDate and endDate was possible. However, my question is: Is ...

Issue encountered while utilizing combineReducers: "Error: The assetsReducer returned an undefined value during initialization."

Issue: The "assetsReducer" has returned an undefined value during initialization. When the state passed to the reducer is undefined, it must explicitly return the initial state, which cannot be undefined. If no value is set for this reducer, consider using ...

Enhance user experience with Jqgrid hyperlinked user format

I am currently utilizing jqgrid with JSON data that is structured as follows: {"my_id" : 12345, "doc_number" : 9786} My objective is to implement an hlink formatter that displays the doc_number, while passing the my_id as a parameter to my JavaScript func ...