Struggling to retrieve the 'this' object using a dynamic string

Within my Nuxt + TS App, I have a method that attempts to call a function:

nextPage(paginationName: string): void {
      this[`${paginationName}Data`].pagination
        .nextPage()
        .then((newPage: number) => {
          this.getData(paginationName, newPage);
        });
    },

I am trying to access my computed property using 'this', but the compiler is throwing an error stating

Element implicitly has an 'any' type because expression of type 'string' can't be used to index type
. While using @ts-ignore resolves the issue, I want to find a better solution. Here is the computed property in question:

soonData(): ISoonData {
      const soonData = this.$store.getters['soonData'];
      const pagination = new Pagination(
        Number(soonData.currentPage),
        soonData.maxPage
      );
      return new soonData(soonData, pagination, 'soon');
    },

My components are declared using Options-api with export default Vue.extend({})

Answer №1

I managed to figure it out by replacing the context as such:

let _self: any 

export default Vue.extend({
...

methods: {
  nextPage(paginationName: string): void {
  _self[`${paginationName}Data`].pagination
    .nextPage()
    .then((newPage: number) => {
      this.getData(paginationName, newPage);
    });
},
}
...
created () {
  _self=this
}

})

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

Getting the item that was clicked on a Chart in a PrimeNG chart within an Angular application can be achieved by following these

I am trying to implement a bubble chart and I would like the function to be called when a user clicks on one of the bubbles. What is the best way for me to pass the data to this function? https://i.stack.imgur.com/FYiSP.png <p-chart type="bubble" [da ...

The property you are trying to access is not defined on the enum type in Types

Looking to revise the TypeScript syntax of a lesson found at this link. I am aiming to extract a specific type from a union type using the following syntax: Actions['type'][ActionTypes.FEED_CREATE_POST] The available action types are defined a ...

Can anyone explain why the Splice function is removing the element at index 1 instead of index 0 as I specified?

selectedItems= [5,47] if(this.selectedItems.length > 1) { this.selectedItems= this.selectedItems.splice(0,1); } I am attempting to remove the element at index 0 which is 5 but unexpectedly it deletes the element at index ...

Securing access with Vue.js authentication even if the URL is entered manually

Within my main.js file, I have configured my routes array to include: meta : { requiresAuth: true } for all components except the UserLogin page. For navigation resolution, I have implemented a check before each route: router.beforeEach((to, from, next ...

Error message "Property 'name' does not exist on type '{}'" is encountered when using Ionic/Angular HttpClient and no data type is specified

While working on my Ionic project, I encountered an error in Angular when trying to fetch data from an API using HttpClient. The error message that popped up was 'Property 'name' does not exist on type '{}'.'. Below is the cod ...

Accessing information necessitates two separate subscriptions

I am posting this inquiry in order to enhance my understanding. Below is an excerpt from my service: export class HomeService { private generalstatistics = new ReplaySubject<object>(); constructor( private http: HttpClient ) { this ...

Launching a new tab with a specific URL using React

I'm attempting to create a function that opens a new tab with the URL stored in item.url. The issue is, the item.url property is provided by the client, not by me. Therefore, I can't guarantee whether it begins with https:// or http://. For insta ...

Exploring the capabilities of AWS Cognito with Vue.js

I recently created a backend API using Expressjs hosted on AWS EC2. I also developed a frontend application in Vuejs that interacts with the Express API. My next step is to implement user Authorization for sign-ins, and after some research, I've deci ...

"Encountered a type error with the authorization from the credentials provider

Challenge I find myself utilizing a lone CredentialsProvider in next-auth, but grappling with the approach to managing async authorize() alongside a customized user interface. The portrayal of the user interface within types/next-auth.d.ts reads as follo ...

Is it feasible to append an element to the result of a function that returns an array?

Is it possible to push something to an array returned by a function, but not directly? Instead, I want to push it back into the function itself. hierar() { return [{ h: 1 }, { h: 2, hh: [{ u: 2.1 }, { u: 2.2 }] }, { h: 3, hh: [{ u: 4 }, { U: 5 } ...

Unable to execute a join operation in TypeScript

I have an array of objects listed below var exampleArray = [{ "isAvailable": true, "receipent": [{ "id": "a6aedf0c34", "receipentName": "ABC" }, { "id": "a6aedbc34" ...

What is the process for integrating MongoDB into Vue.js 2?

I've been trying to install mongodb through npm, but I keep encountering the error message "Cannot find module 'fs'." Here is a snippet of my code: <script> const MongoClient = require('mongodb').MongoClient; export defau ...

Exploring the world of HTTP PUT requests in Angular 4.0

I have encountered an issue with a function I wrote for sending an http put request to update data. The function is not receiving any data: updateHuman(human: Human) { const url = `${this.url}/${human.id}`; const data = JSON.stringify(human); ...

How can I dynamically replace a specific element inside a .map() function with a custom component when the state updates, without replacing all elements at once?

I'm currently developing a task management application and I need to enhance the user experience by implementing a feature that allows users to update specific tasks. When a user clicks on the update button for a particular task, it should replace tha ...

Styling of Bootstrap HTML element not appearing as expected

Recently, I've been trying out a new approach by embedding Bootstrap components in an iframe. However, despite loading the necessary stylesheet and scripts, the elements seem to be missing their styles. Can anyone shed some light on why this might be ...

Managing onChange in a ReactJs project

Currently, my React tsx page features input boxes like the following: <textarea value={this.state.myData!.valueOne} onChange={(e) => this.handleValueOneChange(e)}/> <textarea value={this.state.myData!.valueTwo} onChange={(e) => thi ...

Issue with esbuild not executing within docker compose configuration

Currently, I am new to using esbuild and struggling to set up a script that can watch and rebuild my files. Additionally, I need this functionality to work within a docker environment. Within my package.json file, the following scripts are defined: " ...

An error of type TypeError has been encountered due to an invalid argument type. This occurred in the file located at {mypath}Desktop eddit ode_modules@redisclientdistlibclientRESP2encoder.js on line

Currently, I am diving into Ben's TypeScript GraphQL Redis tutorial for the first time. As a newcomer to TypeScript, I decided to give Redis a shot. However, when I added the property req.session.userId= user.id;, things took a turn. An error popped ...

Mastering the Art of Utilizing Emit and Props in the Nuxt3 Composition Api

I'm trying to implement emit and props in Nuxtjs3, but it seems like something is off. In my project, I have the need to access airport data across multiple pages, so I created an airportsearch component. This component is located inside components/ ...

using vuejs to pass a function as a prop

As I work on creating a foundational "TableComponent," incorporating selectable rows and more, I am faced with the requirement for this TableComponent to accept a prop named "buttons." These buttons are expected to be in the form of an array of objects ...