Utilize Vuex's $store.getters within a component's method() function

I am facing a challenge in dynamically populating a dropdown list using Vuex and Vuetify for a search box. My issue lies in the fact that I am unable to access the $store within the method() function, only through computed() of course.

Below are my getters/state:

loadedTours:Array[9]
0:Object
1:Object
2:Object
3:Object
4:Object
5:Object
6:Object
7:Object
8:Object
9:Object

which can be fetched using v-for from the computed() function

tours () {
    return this.$store.getters.loadedTours
},

Here is a method() implementation which operates successfully if the list is present in the data():

methods: {
    querySelections (v) {
        this.loading = true
            setTimeout(() => {
                this.items = this.tours.filter(e => {
                    return (e || '').toLowerCase().indexOf((v || '').toLowerCase()) > -1
                })
            this.loading = false
        }, 500)
    }
}

The expected outcome should display the tour titles listed in each object.

Current workaround:

added created():

created () {
    this.loadedTours = this.$store.getters.loadedTours
},

modified method() as follows:

querySelections (v) {
    let that = this; 
    setTimeout(() => {
        that.items = that.loadedTours
    }, 500)
}

Eliminated the arrow function at the data property.

Now aiming to retrieve the tourTitle, then apply input filtering.

Answer №1

Another point to consider, aside from what was mentioned in the comments, is that your primary mistake may stem from using this within the arrow function. This can lead to this not referencing the correct (vuejs) context.

According to the official documentation:

It's crucial to avoid using an arrow function with the data property (for example: data: () => { return { a: this.myProp }}). Arrow functions bind to the parent context, so this will not point to the Vue instance as anticipated, resulting in this.myProp being undefined.

An alternative approach would be:

  let that = this; 
  setTimeout(() => callback(that.name), 15); 

By doing this, that will correctly reference the vuejs this object.

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

Hover over buttons for a Netflix-style effect

https://i.stack.imgur.com/7SxaL.gif Is there a way to create a hover effect similar to the one shown in the gif? Currently, I am able to zoom it on hover but how can I also incorporate buttons and other details when hovering over it? This is what I have ...

What is the reason behind Vue not updating the array order in $ref when unshift is used?

Here's an interesting example to ponder: <template> <div> <button @click="addItem">Add Item</button> <h1>Fruits</h1> <ul> <li v-for="(item, index) in items" ...

Don't use onchange() in place of keyup()

Issue: I am facing a problem where the keyup() function is calling ajax multiple times with each key press, and I have tried using onChange() but it did not work as expected. Here is the code to check if an email already exists in the database: $.noConf ...

Creating a dynamic directive in AngularJS: Learn how to add or remove it from the DOM based on specific conditions

Suppose I have a customized modal directive that looks like this: return { restrict: 'E', templateUrl: 'modal.html', scope: { modalContent: "&", modalOpen: "&" } } And in the HTML: <ng-modal modal-content="co ...

What does Event.target.id return - the HTML id value or the HTML name value?

HTML Form Example: <form id="form-product-add" style="display: inline;"> <input name="csrf" type="hidden" value="<?= $this->csrf; ?>"> <input type="hidden" name="a ...

Is there a way to merge two arrays with a specific condition?

I need to update the state in one array based on the values of another array. The goal is to include all statuses from the second array into the first without any duplicates. However, the provided sample code is not comprehensive. const arr1 = [{ agentId: ...

`Warning: The alert function is not working properly in the console error

I am currently working on integrating otp functionality into my Ionic 3 project. I am facing an issue where I am able to receive the otp, but it is not redirecting to the otp receive page due to a specific error. Below is the console error that I am encou ...

Adjust the href link value when a new option is selected

I currently have a select element displayed below. Next to it, there is a link that sets the eID value to a session value. However, I want this value to be updated dynamically whenever a different eID value is selected from the dropdown. Although I can r ...

Nunjucks not loading the script when moving from one page to another

Currently, I am in the process of developing a website utilizing nunjucks and express. This website serves as a blog platform with content sourced from prismic. My goal is to load a script file for an active campaign form whenever a user navigates from a b ...

VueJS does not show a component if it is contained within a v-if or v-show directive

My goal is to show a component when the 'Add Patient' button is clicked using a v-if directive: // within <template></template> <button type="button" class="btn btn-info" @click="showPatientForm">Ad ...

Minimize all expanded containers in React

Although I've come across similar questions, none of them have addressed mine directly. I managed to create a collapsible div component that expands itself upon click. However, I am looking for a way to make it so that when one div is expanded, all o ...

An error was encountered: The object 'object object' does not have the 'on' method available

I want to experiment with this website: However, I am encountering an error without any events triggering. I am confused why the method "on" is not being found even on page load. <head> <link href="css/scrollable-horizontal.css" rel="styleshe ...

Using TypeScript to define values with the placeholder "%s" while inputting an object as a parameter

One common way to decorate strings is by using placeholders: let name = "Bob"; console.log("Hello, %s.", name) // => Outputs: "Hello, Bob." I'm curious if there's a way to access specific values within an object being passed in without specif ...

Is it possible for a Vuex module to observe or monitor another Vuex module?

Is it possible for Vuex modules to monitor the state of other modules and accordingly trigger actions? Consider this scenario: store.js import time from './store/time' ; import position from './store/position' ; const store = new Vu ...

Sending data to HTML using parameters in a .cshtml document

I encountered a situation where I had to deal with the following code snippet within a .cshtml file: var peopleList = $('#PeopleListTable').dataTable({ // not relevant "fnRender": function (oObj) { ...

The Philosophy Behind Structuring Node.js Modules

There appears to be a common understanding regarding the directory structure in node.js, but I have not come across any official documentation on this topic. Based on my exploration of open source projects, it seems that most projects typically include a ...

Issues with navigation menus in Bootstrap/Wordpress

My mobile drop-down menu is giving me some strange issues. When you toggle the button, the menu briefly appears just below the button before moving to its correct position. Sometimes it doesn't work at all, and clicking has no effect. You can witnes ...

What advantages do constant variables offer when selecting DOM elements?

What is the significance of declaring variables as constant when selecting elements from the DOM? Many developers and tutorials often use const form = document.querySelector('form'); ...

What is the best way to incorporate the {id} property into my Next.js dynamic route using the Context API?

I am encountering an issue with passing the ${id} property to my dynamic route [id]>page.jsx, located within the CartItemPage.jsx file. The setup involves using the Context API, similar to React, where I maintain an array of CartItems to render the Cart ...

When an object is pushed into an array, it gets duplicated and also appears in a proxy when viewed in the console

Working with Firebase 9 and Vue 3 in building a chat application. The issue at hand is that when I push message objects to the messages array [], the console shows duplicates like this: Proxy {0: {…}, 1: {…}} [[Handler]]: Object [[Target]]: Array( ...