W
W
wakenby2021-01-08 21:34:10
Vue.js
wakenby, 2021-01-08 21:34:10

How to name classes in vue components?

How to write classes in vue components correctly? With component name prefix? (meaning best practice)

Option 1:

// TodoItem.vue
<template>
  <div class="todo-item">
    <div class="todo-item-left">...</div>
    <div class="todo-item-button">...</div>
  </div>
</template>


Option 2:
// TodoItem.vue
<template>
  <div class="todo-item">
    <div class="left">...</div>
    <div class="button">...</div>
  </div>
</template>


Option 3:
// TodoItem.vue
<template>
  <div class="todo-item">
    <div class="todo-item__left">...</div>
    <div class="todo-item__button">...</div>
  </div>
</template>


In the second option, there is an unpleasant feature if there is a Title.vue component with the content:
// Title.vue
<template>
  <div class="title">Заголовок</div>
</template>


And we import it into another component, which already has its own title:
// Home.vue
<template>
  <div class="home">
    <Title/> // Конфликт c class="title"
    ....
    <div class="title"></div>
  </div>
</template>


Then the Title.vue component will have a class conflict, it will be affected by styles from Title.vue, and from Home.vue

What is a good practice for naming classes?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry, 2021-01-08
@DKWLB

Try the second option if you like, but add scoped in the component
In theory, the styles will not overlap
<style scoped>

A
Alexey, 2021-01-10
@AlexeyCaTHaR

BEM? Those. Block(functional)-element-modifier. There should be no intersections due to the fact that the functional blocks are not duplicated in DIFFERENT components (i.e. for one functional block one component)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question