Answer the question
In order to leave comments, you need to log in
Which version of the component is objectively better?
Good evening.
I made an accordion component using named slots (don't say that there is a better one, it's just that on my current gallery they basically saw their bikes). In my opinion, it turned out very convenient and beautiful. I’ll omit the implementation code (it doesn’t matter within the framework of the question), the use looks like this:
<v-accordion
:items="addresses"
>
<template v-slot:item="{ item }">Item {{ item.id }}</template>
<template v-slot:content>
Lorem ipsum dolor sit amet, consectetur adipisicing elit
</template>
</v-accordion>
<v-accordion>
<v-accordion-item v-for="item in items">
<div v-slot="title">
{{ item.title }}
</div>
<div>{{ item.content }}</div>
</v-accordion-item>
</v-accordion>
Answer the question
In order to leave comments, you need to log in
The arguments of your team leader are unprofessional and not supported by rational argumentation. Your approach maintains the component structure of the application. While what your team leader suggested makes you remember too much about how to code, increases the amount of repetitive code, generally makes it more imperative, which is not compatible with the ideas behind Vue. In general, I advise you to insist on your own and describe why your approach is better. “I have always done this, we are so used to it” is not an argument. Your team leader clearly does not see the abstract side and does not understand the work of the framework well, or maybe, as you say, he is not very professional.
UPD.
I researched the comments here, thought and came to additional conclusions, which I will describe here:
1) Your approach has a minus - it is redundant, since it contains two slots, which makes the logic unclear. The fact is that you do not understand what the second content slot refers to. The reader might think that this is the content that the accordion itself outputs - once, and not for each element. I am especially familiar with such logic from the WPF framework. Everything works there, but judging by your example, this slot belongs to the item slot, that is, it is used not for the accordion, but for its elements, which is wrong, as it violates encapsulation. This is a minus of your approach, but it is easily corrected.
2) The disadvantages of your team lead's approach remain the same - verbosity, imperativeness. It violates the DRY principle.
So, an approach is required that will not violate best practices - a hybrid approach. First, we need to separate the concepts of an accordion and an accordion element - now these are two loosely coupled components. We should be able to change the accordion element component to a new one without touching the accordion itself. This is solved very simply. We just take your approach and leave only one item slot in it. This slot will allow the user of the accordion to create the markup of their choice. We remove the content slot as unnecessary. In the future, we can add more slots to the accordion and they will change its structure, for example, add a title to it or something else, but at the same time not touching how the interior of its elements is displayed (remember about encapsulation)! This should be done by the items slot. Now, we have the ability to create the markup of its elements. We can do this with simple HTML tags if we have the accordion in one place and forget about the next (remember the principles of YAGNI and KISS). If we have an accordion in several places, and, moreover, the markup of its elements is the same each time, then it is worth thinking about the component of the accordion element. Its structure is not important, so I won't go into details. Next, we simply create another component that will wrap our accordion and substitute the corresponding component of the accordion element into its item slot (and, possibly, add some more functionality, markup) - like creating a subclass. This way we do not write v-for every time, that is, we do not repeat ourselves (remember about DRY), and we have full control over what happens in the application. If you need to change something for everyone, we'll just go in and change that in our final wrapper component. If we need to implement something new, then we will create a new wrapper or even use our basic accordion on the desired page (remember YAGNI). Some may find this to be redundant. It is, and it must be used wisely. If you have one accordion for the entire application, then thinking about reusing it is pointless. If the component is required in several places, then the approach I have given is perfect, as it is based on the best practices taken from OOP. If you have one accordion for the entire application, then thinking about reusing it is pointless. If the component is required in several places, then the approach I have given is perfect, as it is based on the best practices taken from OOP. If you have one accordion for the entire application, then thinking about reusing it is pointless. If the component is required in several places, then the approach I have given is perfect, as it is based on the best practices taken from OOP.
PS With the wrapper approach, we don't even need to write separate components for the accordion elements, since, in principle, we don't need the accordion elements separately from it. All you have to do is create a child accordion with the desired markup in the item slot.
Isn't it possible to write it like a human being?
I don’t know Vue, but from the options offered, I imagine it somehow like this
<accordion :items="items">
<!-- add your content layout,
in our case this is div -->
<div>{{item.content}}</div>
</accordion>
<v-accordion :items="items">
<v-title>{{title}}</v-title>
<v-content>{{content}}</v-content>
</v-accordion>
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question