LoginSignup
0
0

More than 5 years have passed since last update.

【Vue.js】Vueのコンポーネントはカスタムタグで描画されない

Last updated at Posted at 2019-02-14

はじめに

vue-loaderでscoped cssを書くと親のスタイルが子コンポーネントのルート要素に適応される。
これにより稀に困ったことが起こる。

どんな時に困るのか?

App.vue
<template>
  <div class="container">
    <sample-component />
    <div>hoge</div>
  </div>
</template>

<script>
import SampleComponent from './SampleComponent.vue'

export default {
  components: {
    'sample-component': SampleComponent
  }
}
</script>

<style scoped>
.container {
  display: flex;
}
</style>
SampleComponent.vue
<template>
  <div class="container">
    <div>fuga</div>
    <div>fuga</div>
  </div>
</template>

<style scoped>
.container {
  font-size: 100px;
}
</style>

上記の場合、SampleComponent.vue にもcontainerにも displey: flex のスタイルがあたってしまう。

スクリーンショット 2019-02-14 12.10.49.png

解決策

scopedと言えど命名規則は決めておく
Vueコンポーネントに合わせたCSSの命名規則を考えた

CSS Modulesを使う

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0