LoginSignup
2
5

More than 5 years have passed since last update.

VueでCSS Modulesを使っているCSSファイルをインポートする方法

Last updated at Posted at 2018-03-28

styleを外部ファイルからインポートする

公式サイトに書いてある、ソースのインポート方法。
しかし、CSS Modulesを使っている場合、上のような書き方では、ネストしたスタイルが反映されません。

<template>
  <div class="example">
    <p>{{ msg }}</p>
  </div>
</template>

<script>
export default {
  data () {
    return {
      msg: 'Hello world!'
    }
  }
}
</script>

<style src="./style.css" module></style>
style.css
.example {
  # ネストしたPタグへのスタイルは反映されない。
  & p {
    color: red;
  }
}

対応策

CSS Modules使う場合は以下のように、外部CSSをインポートしましょう

<template>
  <div class="example">
    <p>{{ msg }}</p>
  </div>
</template>

<script>
export default {
  data () {
    return {
      msg: 'Hello world!'
    }
  }
}
</script>

<style module>
  @import './style.css';
</style>
2
5
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
2
5