Using the index in Vue.js to locate a method within an HTML file

Currently, I am attempting to make use of the reference "index" located within

<tr v-for="(note, index) in noteList" v-bind:key="index"
in order to call shareToPublic(index). The method selectedID() allows for the selection of the index and retrieval of the current ID number. Upon clicking the Share button, it should redirect me to a page displaying the content selected based on the ID.

<template>
  <div>
    <button class="btn-delete" @click="shareToPublic(index)">Share</button>    
    <tbody>
      <tr v-for="(note, index) in noteList"
        v-bind:key="index"
        v-if="note.workspace_id === currentWorkspace"
        @dblclick="getNote(note.id)"
        @click="selectedId(index)" >
        <td>{{ note.title }}</td>
        <button type="submit" @click="deletePage(note.id, index)">Delete</button>
      </tr>
  </div>
</template>
<script lang="ts">
  @Componet
  export default class ValueHub extends Vue {
  
    private selectedId(index: number): number {
      return this.noteList[index].id
    }
  
    async shareToPublic(index: number){
      const numberToString = this.selectedId(index).toString()
      await this.$router.push({name: 'PublicContent', params: {id: numberToString}});
    }
  }
</script>

Answer №1

Assuming the issue does not lie within typos or missing code related to nodeList, establish a variable named selected to hold the selected id value (not the index):

export default class ValueHub extends Vue {
  selected: number | null = null;  // This will store the chosen id
  ...
}

Assign the id to selected within the selectedId method:

private selectedId(id: number) {
  this.selected = id;
}

When using @click, ensure that you pass the id instead of the index:

@click="selectedId(note.id)"

Utilize the selected variable to represent the id when performing a router push operation:

async shareToPublic(){
  await this.$router.push({
    name: 'PublicContent',
    params: { id: this.selected }
  });
}

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

Data fetched using React Query

When using React Query to fetch data, the function runs smoothly. After console.logging the 'data' variable from React Query, it prints an array of objects as expected and handles states efficiently between loading, success, error. The issue ar ...

Combining webpack encore and Symfony, we can easily integrate VueJS single file components (SFC) into our project's style

Working on a Symfony5 project, I am incorporating the VueJS framework using Single File Components (SFC). Within my VueJS component styles, I need to utilize the "url" css rule to define file paths. Specifically for designing a div with background-image ...

I encountered an error in my Node.js application stating that it could not find the name 'Userdetailshistory' array. I am puzzled as to why this error is occurring and I suspect it may be due to my

import { Component, OnInit } from '@angular/core'; import { UserdetailshistoryService } from '../../services'; @Component({ selector: 'my-userdetailshistory', templateUrl: './userdetails-history.component.html', ...

How is it possible for Typescript to let me create an object without explicitly defining all mandatory fields?

After creating a class and instantiating an object from it through the constructor, I expected to receive an error message indicating missing properties. However, despite the fact that the class description specified required fields, I was able to create a ...

The user's type from express-session is not being properly detected by Typescript

I have a process where I retrieve the user object from the database and set it on the express-session: export const postLogin = async ( request: Request, response: Response, next: NextFunction ): Promise<void> => { try { re ...

Chrome Not Responding to Angular5 Debugging

I'm facing an issue where I used to be able to set breakpoints in my Angular code using developer tools, and it would pause correctly. However, recently the network files are not being mapped to my local files properly. For a detailed explanation, ple ...

Angular: issue with form validation - password matching is not functioning as expected

I have successfully implemented the registration form with basic validations The code I used can be found in: registration.html <form #registrationForm="ngForm" (ngSubmit)="onFormSubmit()"> ... <div class="form- ...

The module "angular2-multiselect-dropdown" is experiencing a metadata version mismatch error

Recently, I updated the node module angular2-multiselect-dropdown from version v3.2.1 to v4.0.0. However, when running the angular build command, I encountered an "ERROR in Metadata version mismatch for module". Just to provide some context, I am using yar ...

Conceal components using routing in Angular2 release candidate 1

I have a request regarding certain elements that are to be displayed on all pages except the login page. I am considering using either ngIf or the hidden property of the elements to hide them when the user is on the login page. Here is what I have attempt ...

Displaying a component in a router view based on specific conditions

Currently, I am delving into the world of Vue3 as a newcomer to VueJS. In my project, there is an App.vue component that serves as the default for rendering all components. However, I have a Login.vue component and I want to exclude the section when rende ...

Toggle the accordion with just the icon and include a click event on the button

I'm looking to customize the accordion functionality by toggling it with icons only, while also adding a click event on the buttons themselves. I've attempted the following code snippet-- <html> <link href="https://cdn.jsdelivr.net ...

Vuetify Event Calendar

Currently, I am using the Vue calendar with Vuetify version 2.1.x. I am trying to retrieve the time from a specific slot when it is clicked like this: @click:time="test" <v-calendar ref="calendar" :locale="$i18n.locale" v-model="schedulerObj.calV ...

How do I navigate to a different page in Vue.js HTML based on user selection?

Here is my first attempt at writing HTML code: <div class="col-md-4" > <div class="form-group label-floating"> <label class="control-label">Select No</label> <select class="form-control" v-model="or" required=""> ...

Is it possible to set the duration of transitions in Vue.js to

Currently, I am working with the official Vuejs router and aiming to implement unique animations for different routes. I found an example here: https://router.vuejs.org/en/advanced/transitions.html (look under 'Route-Based Dynamic Transition') H ...

Is there a way to attach a ref to a Box component in material-ui using Typescript and React?

What is the best way to attach a ref to a material-ui Box component in React, while using TypeScript? It's crucial for enabling animation libraries such as GreenSock / GSAP to animate elements. According to material-ui documentation, using the itemRef ...

Utilizing handpicked information in Angular: A beginner's guide

Being new to Angular and programming in general, I am currently navigating through the intricacies of Angular and could use some guidance on utilizing selected data. For instance, I would like to use a personnel number view image here and send it to the b ...

Can someone provide instructions on how to convert base64 data to an image file

I'm utilizing the vue-signature Library but I am unsure how to download the base64 data that is generated as an image. Here is the link to the library: https://www.npmjs.com/package/vue-signature. I have gone through the documentation and noticed that ...

I encountered an issue with Angular where it is throwing an error stating that the property 'catch' does not exist on the type 'Observable<Object>'

I have been working on an angular application that interacts with a python flask API. During development, I encountered the need to display results passed from the backend. To achieve this, I created an angular service. Below is the code for the angular s ...

What is the best way to trigger a function when a chart changes in Nuxt using Vue JS?

How can I trigger a function whenever a chart is loaded or changed in Vue Nuxt JS? I've tried using created, mounted, updated, and beforeMount but none of them seem to work. Can anyone offer any guidance? Edit 1: Here's the code snippet: upd ...

Looking for a solution to the TypeScript & Mantine issue of DateValue not being assignable?

The required project dependencies for this task are outlined below: "dependencies": { "@mantine/core": "^7.6.2", "@mantine/dates": "^7.6.2", "@mantine/form": "^7.6.2", &q ...