2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【wepbak】vue&type=style&index=0&lang=css

Last updated at Posted at 2018-11-12

vue&type=style&index=0&lang=css

Error | エラー

Vue の single file component に style を書き込むと以下のエラーが出ることがある。

ERROR in ./src/App.vue?vue&type=style&index=0&lang=css& (./node_modules/vue-loader/lib??vue-loader-options!./src/App.vue?vue&type=style&index=0&lang=css&) 14:3
Module parse failed: Unexpected token (14:3)
You may need an appropriate loader to handle this file type.

./src/App.vue の部分はディレクトリ構造によります。

Solution | 解決方法

style-loadercss-loader をインストールし、webpack.config.js に設定を記述する。

  1. インストール

    # using npm
    npm install style-loader css-loader --save-dev
    
    # using yarn
    yarn add style-loader css-loader --dev
    
  2. webpack.config.js へ以下の記述を追加

    module.exports = {
      module: {
        rules: [
          {
            test: /\.css$/,
            use: ["style-loader", "css-loader"]
          }
        ]
      },
    }
    
  3. single file component サンプル

    <template>
      <div>
        <h1>Hello World!</h1>
      </div>
    </template>
    
    
    <script>
    </script>
    
    
    <style>
    h1 {
      font-size: 18px;
    }
    </style>
    

Supplement | 補足

<style> は、<style scoped>とすれば data 属性が付与されたスコープされたスタイルとなる他、設定を追加すれば scss なども利用可能です。

2
1
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
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?