jade
jade

Reputation: 33

how to access result of method in vue

I want to pass a result of a method to params. Here's my code

<template>
    <router-link
        v-bind:to="{
            name: 'faq-page',
            params: { id: selectedCategory },
        }"
    >
         <li><a @click="findCategory(eachQuestion.id, faqData)">{{eachQuestion.question}}</a></li>
    </router-link>
</template>

<script>
export default {
    data() {
        return {
            selectedCategory: ''
        }
    },
    methods: {
        findCategory(id, list) {
            const x = //function here
            if (x) {
             return this.selectedCategory = this.x.slug;
            }
        }
    }
}
</script>

The idea of this code is, whenever a user click the <li>, the method will be executed to find x.slug. Then I want to pass the x.slug in the params. I believe I did something wrong with the code. What's the correct way to pass the method value to the params? Thanks so much.

Upvotes: 0

Views: 60

Answers (1)

Noy Gafni
Noy Gafni

Reputation: 1201

Your method should not return the a value, and x is a local variable so you shouldn’t use this.x:


methods: {
        findCategory(id, list) {
            const x = //function here
            if (x) {
             this.selectedCategory = x.slug;
            }
        }
    }

Upvotes: 1

Related Questions