Using css-loader to implement css module in vue-cli

Using css-loader to implement css module in vue-cli

【Foreword】

Both Vue and React's CSS modular solutions rely on loaders to implement them. In use, Vue uses scoped attributes to privatize styles, and uses deep selectors /deep to deprivatize styles.

example:

<div>
    <div class="demo">
        <div class="child"></
        div>
    </div>
</div>
<script>
//some code
<script/>
<style lang="less" scoped>
  .demo {
    height: 100px;
    padding-top: 20px;
    background-color: grey;
    /deep/.child {
      color: red;
    }
  }
</style>

This is how it is used in react (based on css-loader):

//test.less
.demo {
    height: 100px;
    padding-top: 20px;
    background-color: grey;
    :global(.child) {
        color: red
    }
}
import styles from './test.less'

//some code

<div className={styles.demo}>
    <div class="child"></div>
</div>

I have to say that vue is more convenient to use.

What if you insist on using css-loader in vue to implement this solution of css module? Here we take vue-clie 3.x as an example.

Whether in vue's scaffolding vue-cli or in react's scaffolding umi , webpack-chain is now used to configure webpack.

Here, in the root directory of the project created by vue-cli scaffolding, create a new vue.config.js and write the following content:

module.exports = {
  chainWebpack: (config) => {
    config.devServer
      .proxy({
        '/api': {
          target: 'http://localhost:3000',
          pathRewrite: { '^/api': '', },
        },
      })
      .port(9000);

    config.module
    .rule('less')
    .oneOf('normal-modules')
    .test(/.less$/)
    .use('css-loader')
    .tap(options => {
      return Object.assign(options, {
        modules:
          localIdentName: '[name]__[local]___[hash:base64:5]',
          auto: /\.less$/i,
        },
      })
    });

  },
};

Actually, you don't need to write this paragraph. By default, the scaffolding of vue-cli has configured the modularization of css-loader , but the less file needs to be named in the form of xxx.module.less , which is different from the umi set and inconvenient. After configuring and restarting, you can write css like react, and store the imported style in data . Here I just want to talk about the solution that can use css-loader in vue-cli, but the best practice is to use the one that comes with vue .

This is the end of this article about using css-loader to implement css module in vue-cli. For more information about using css module in vue-cli, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

<<:  Html page supports dark mode implementation

>>:  Nginx configuration SSL and WSS steps introduction

Recommend

How to handle forgotten passwords in Windows Server 2008 R2

What to do if you forget Windows Server 2008R2 So...

Vue implements sample code to disable browser from remembering password function

Find information Some methods found on the Intern...

Six tips to increase web page loading speed

Secondly, the ranking of keywords is also related ...

JS implements click drop effect

js realizes the special effect of clicking and dr...

Detailed explanation of MySQL remote connection permission

1. Log in to MySQL database mysql -u root -p View...

Methods and steps for deploying multiple war packages in Tomcat

1 Background JDK1.8-u181 and Tomcat8.5.53 were in...

5 ways to migrate from MySQL to ClickHouse

Data migration needs to be imported from MySQL to...

...

MySQL 8.0.12 winx64 decompression version installation graphic tutorial

Recorded the installation of mysql-8.0.12-winx64 ...

CentOS 7.x docker uses overlay2 storage method

Edit /etc/docker/daemon.json and add the followin...

The textarea tag cannot be resized and cannot be dragged with the mouse

The textarea tag size is immutable Copy code The c...

Detailed explanation of nginx optimization in high concurrency scenarios

In daily operation and maintenance work, nginx se...