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

What is the best way to create nested inline template components?

In my web application, I have an <overview> component that stores all contact information in a prop. When displaying the contacts, I loop through them within the overview and create table rows to showcase each one individually. However, for specifi ...

How can we limit the CSS properties that can be used in an interpolated manner by defining a restricted TS type for CSS props based on emotions?

When dealing with emotions, how can we specify a restricted TS type for the css prop to only allow certain css properties to be interpolated? For instance, consider the following scenario: // This is considered valid css = {{ color: 'white', ...

Using ngIf to validate an empty string in Angular 5

I need assistance with validating an empty string retrieved from a server Although it is usually straightforward, it's just not working as expected <div class="ui-g-2 info-txt" *ngIf="appointment.Notes !==null || appointment.Notes !== ...

I am encountering an issue trying to create a Docker image featuring TypeScript

I am facing an issue while trying to build a docker image using the docker build . command in my next.js app An error is being encountered Error: buildx failed with: error: failed to solve: process "/bin/sh -c yarn run build" did not complete su ...

How can I dynamically change and load a configuration file based on the URL parameter using Angular?

My query involves modifying the config file on pageload based on a URL parameter. I currently have implemented the following: config-loader.service.ts @Injectable() export class ConfigLoaderService { constructor(private injector: Injector, private ht ...

Is there a way to automate downloading a file in Angular using the browser's built-in download feature?

When I make a request to my webservice to download a zip file, the file content is downloaded secretly and suddenly appears in the download task bar as already fully downloaded (100%). I am using the following method in Angular: const endpoint = "http:// ...

Use Angular2 to showcase the selected image as the main one when the user clicks on the

I'm working on creating a product thumbnail gallery, and I'd like the main image to be displayed when the user clicks on a thumbnail. I am using Angular for this project, although I am still learning my way around the framework. product.html &l ...

Animate the transition of the previous element moving downward while simultaneously introducing a new element at the top

I currently have a hidden element called "new element" that is controlled by v-if. My goal is to create a button labeled "display" that, upon clicking, will reveal the new element on top after sliding down an old element. How can I achieve this using CSS ...

Compiler unable to determine Generic type if not explicitly specified

Here is a simple code snippet that I am working with: class Model { prop1: number; } class A<TModel> { constructor(p: (model: TModel) => any) {} bar = (): A<TModel> => { return this; } } function foo<T>(p: ...

Navigating through pages: How can I retrieve the current page value for implementing next and previous functions in Angular 7?

Greetings, I am a new learner of Angular and currently working on custom pagination. However, I am facing difficulty in finding the current page for implementing the next and previous functions. Can anyone guide me on how to obtain the current page value? ...

Error message: Typescript class unable to access methods because of undefined properties (TypeError: Cannot read properties of undefined (reading 'method_name'))

During the compilation process, everything goes smoothly with no errors. However, when I try to call the method in the controller, an error occurs. It seems that some class methods are missing during the compilation phase. If you require more information ...

Taking out the modal element from the document object model, causing the animation

I am currently working on a project using Next.js, Typescript, and Tailwind CSS. Within this project, I have implemented a modal component with some animations. However, I encountered an issue where the modal does not get removed from the DOM when closed, ...

Utilize the global theme feature within React Material-UI to create a cohesive

I'm feeling a bit lost when it comes to setting up React Material-UI theme. Even though I've tried to keep it simple, it's not working out for me as expected. Here is the code snippet I have: start.tsx const theme = createMuiTheme({ ...

Contrast: Colon vs. Not Equal Sign (Typescript)

Introduction Hello everyone, I am new to Typescript and currently grappling with some fundamental concepts. When defining a parameter for a function, I typically specify the type like this: function example(test: string){...} However, as I delve deeper ...

Switching over to Typescript: Sending mapped properties

I'm having trouble converting my React.JS script to TypeScript. I need assistance in creating a drop-down navigation bar on my website. Here's a snippet from my Header.tsx file: The error message Property 'onClick' does not exist on t ...

What is the best way to manage optional peer dependency types while releasing a TypeScript package?

I'm trying to figure out the best way to handle optional peer dependencies when publishing a TypeScript package on npm. My package provides a function that can accept input from either one of two peer dependencies. How should I define these optional p ...

Is it possible to execute TestCafe tests using TypeScript page objects that have not been utilized?

While working with TestCafe, I am implementing tests using the Page Objects pattern. I have already written some page objects in advance, even before their actual usage, as I am familiar with the page and know what to anticipate. However, when attempting ...

Picked (chosen-js): Steps to deselect options using a variety of selectors

I am currently using the Chosen library on Vue and Webpack for my project. I have a scenario where I need to cancel a selection when multiple options are selected, but I am struggling to achieve this. Can anyone guide me on how to cancel a selected optio ...

Having trouble adding the Vonage Client SDK to my preact (vite) project

I am currently working on a Preact project with Vite, but I encountered an issue when trying to use the nexmo-client SDK from Vonage. Importing it using the ES method caused my project to break. // app.tsx import NexmoClient from 'nexmo-client'; ...

How can the navbar be kept persistent in vue-router?

Coming from a background in React, I am currently learning Vue and have encountered an issue regarding the persistence of a navbar with router links. My navigation bar component works fine, but it keeps reloading whenever I navigate to a different route. I ...