-2

I want to insert HTML tag inside map method in Vuejs. But it doesn't work with this way. The anchor tag is displayed as plain text.

<template v-if="category.sub_category.length > 0">
                        <td>
                          {{ category.sub_category
                          .map(
                            ({ subcategory_name, sub_sub_category }) =>
                              subcategory_name +  '<a href="" @click.prevent="showAssignModal(category)">' + `(${sub_sub_category.length})` + '</a>'
                          )
                          .join(', ') }} 
                        </td>
                    </template>
6
  • 1
    This isn't React, unless you specifically use render function. Use v-for instead of map. Commented Jul 27, 2021 at 10:48
  • But if I use v-for it's difficult for me to use .join(', '), as I mentioned in stackoverflow.com/questions/68528162/… Commented Jul 27, 2021 at 10:52
  • 1
    The previous question was XY problem because you need to output HTML while the question stated that it's plain text. Don't use join then if it's difficult. You're trying to do this in a way that isn't suitable for Vue. You can move all calculations to a computed with the structure that is easy to use with v-for. Commented Jul 27, 2021 at 11:00
  • 1
    So, the solution is move all calculations to a computed with the structure that is easy to use with v-for.. How do i do? Commented Jul 27, 2021 at 12:37
  • Sorry, I don't have time to write ready to use code. But you could output comma with something like v-if="!item.isLast". Proceed from that computed property should return an array that has isLast property. Commented Jul 27, 2021 at 12:42

1 Answer 1

1

This is what your are looking for

 <div v-if="category && category.sub_category.length > 0">
        <td v-for="(subCategory, index) in category.sub_category" :key="index">
             {{subCategory.subcategory_name}} 
             <a href="" @click.prevent="showAssignModal(category)">
                {{subCategory.sub_sub_category.length}}
            </a>,
        </td>
    </div>

But as @Estus Flask mentioned, this is not react, you should learn some fundamentals of vue first.

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

2 Comments

That's great. Thank you. I am new in Vue. And I am enjoy learning by doing real project even though sometimes it's pretty hard.
My godness, your solution is like what I've used before. stackoverflow.com/questions/68528162/… I don't realize it!

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.