0

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.

2
  • this.category is not defined in the data. From where do you get it? Commented Apr 28, 2021 at 9:57
  • ah sorry, I mistyped my question. Fixed it already! @NoyGafni Commented Apr 28, 2021 at 10:35

1 Answer 1

1

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;
            }
        }
    }

Sign up to request clarification or add additional context in comments.

5 Comments

I see.. I've tried it but it still doesn't work. Is this line valid params: { id: selectedCategory }?
It suppose to work but maybe try return the hole route to object in a computed property and see if it works
Solved it! Thank u so much!!
so the solution was using it in computed property??
no, my list wasn't imported properly so i had to fix that one. but your answer is true and it helped solve my problem!

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.