1

I am creating several links, that when clicking on them the web should show some images or others, for that, I have some functions within the object methods of vue, each of these functions is responsible for filtering on an array of images and display those that touch. Up to here, everything is correct, the problem is when I have to refer to these functions since the elements that call these functions are created with a v-for in the following way:

<li v-for="categorie in categories">
<a @click="categorie">{{categorie}}</a>
</li>

my reactive variable categories is an array of 5 strings which simply contain names, well, when I click on any of the links I created, I get the following error in the browser console:

vue.js:2006 Uncaught TypeError: fns.apply is not a function
at invoker (vue.js:2006)
at HTMLAnchorElement.fn._withTask.fn._withTask (vue.js:1804)

Of course I have put the same name to the functions that are inside the methods object, in fact if for example I put the following line:

<li v-for="categorie in categories">
<a @click="test">{{categorie}}</a>
</li>

Yes, that works (notice that I have changed the name of the function), but I need to have different function names for each of the elements, to be able to call different functions according to the filter that I want to apply. Could someone help me with this ??? Thank you!!

0

1 Answer 1

1

Have you considered having them all use the same method? And that method then deciding which method to use?

<li v-for="category in categories">
    <a @click="filter(category)">{{ category }}</a>
</li>

and later in methods:

filter(category) {
    // this could be a switch case too
    if(category === 'a') {
        a();
    } else if (category === 'b') {
        b();
    }
}

That said, I'm not sure how the rest of the code looks, but usually I use computed properties to filter on items. so then the computed would look something like this:

filteredItems() {
    this.items.filter((item) item.type = this.currentFilter)
}

and the filter method would be

filter(category) {
    this.currentFilter = category;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.