CSS Modules elegant mode usage
This time I will bring you the use of CSS Modules elegant mode. What are the precautions for using the elegant mode of CSS Modules? The following is a practical case, let’s take a look.
CSS Modules gives each local class a globally unique class name so that component styles will not affect each other. For example:
/* button.css */ .button { font-size: 16px; } .mini { font-size: 12px; }
It will be converted to something like this:
/* button.css */ .buttonbutton--d8fj3 { font-size: 16px; } .buttonmini--f90jc { font-size: 12px; }
When importing a CSS module file, it will provide us with the mapping object of local class name to global class name. Like this:
import styles from './button.css' // styles = { // button: 'buttonbutton--d8fj3', // mini: 'buttonmini--f90jc' // } element.innerHTML = '<button class="' + styles.button + ' ' + styles.mini + '" />'
vue-css-modules: Simplified class name mapping
The following is a that uses CSS Modules Button GroupParts:
<template> <button :class="{ 'global-button-class-name': true, [styles.button]: true, [styles.mini]: mini }">点我</button> </template> <script> import styles from './button.css' export default { props: { mini: Boolean }, data: () => ({ styles }) } </script>
Indeed, CSS Modules is a good choice for Vue components. But there are also the following shortcomings:
You must pass in styles in data
You must use styles.localClassName to import the global class name
If there are other global class names, you must put them together
If you want to bind to the property value of the component, even if it is local The class name is the same as the attribute name, and must also be specified explicitly
For the button component above, after using vue-css-modules:
<template> <button class="global-button-class-name" styleName="button :mini"> 点我 </button> </template> <script> import CSSModules from 'vue-css-modules' import styles from './button.css' export default { mixins: [CSSModules(styles)], props: { mini: Boolean } } </script>
Now:
You don’t have to pass styles in data, but you have to pass styles in mixins:full_moon_with_face:
You can say goodbye to styles.localClassName
Put the local class name in the styleName attribute, and the global class name in the class attribute, which is a lot neater
The local class name binds the component's property with the same name, just add in front of it: Modifier
Modifier
@button
<button styleName="@button">按钮</button>
This is equivalent to:
<button styleName="button" data-component-button="true">按钮</button>
This allows you to reset the style of the component externally:
.form [data-component-button] { font-size: 20px; }
$type
<button styleName="$type">按钮</button>
This is the same as:
<button :styleName="type">按钮</button>
:mini
<button styleName=":mini">按钮</button>
This is the same as:
<button :styleName="mini ? 'mini' : ''">按钮</button> disabled=isDisabled <button styleName="disabled=isDisabled">按钮</button>
This is the same as:
<button :styleName="isDisabled ? 'disabled' : ''">按钮</button>
Usage method
Use in the Vue template
Introduce the CSS module outside the template
<template> <button class="global-button-class-name" styleName="button :mini"> 点我 </button> </template> <script> import CSSModules from 'vue-css-modules' import styles from './button.css' export default { mixins: [CSSModules(styles)], props: { mini: Boolean } } </script>
Use the CSS module inside the template
<template> <button class="global-button-class-name" styleName="button :mini"> 点我 </button> </template> <script> import CSSModules from 'vue-css-modules' export default { mixins: [CSSModules()], props: { mini: Boolean } } </script> <style module> .button { font-size: 16px; } .mini { font-size: 12px; } </style>
Used in Vue JSX
import CSSModules from 'vue-css-modules' import styles from './button.css' export default { mixins: [CSSModules(styles)], props: { mini: Boolean }, render() { return ( <button styleName="@button :mini">点我</button> ) } }
Used in Vue rendering function
import CSSModules from 'vue-css-modules' import styles from './button.css' export default { mixins: [CSSModules(styles)], props: { mini: Boolean }, render(h) { return h('button', { styleName: '@button :mini' }, '点我') } }
I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website !
Recommended reading:
How to write components in vue
##node token implementation verification
The above is the detailed content of CSS Modules elegant mode usage. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Using Bootstrap in Vue.js is divided into five steps: Install Bootstrap. Import Bootstrap in main.js. Use the Bootstrap component directly in the template. Optional: Custom style. Optional: Use plug-ins.

HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

There are two ways to create a Bootstrap split line: using the tag, which creates a horizontal split line. Use the CSS border property to create custom style split lines.

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

To set up the Bootstrap framework, you need to follow these steps: 1. Reference the Bootstrap file via CDN; 2. Download and host the file on your own server; 3. Include the Bootstrap file in HTML; 4. Compile Sass/Less as needed; 5. Import a custom file (optional). Once setup is complete, you can use Bootstrap's grid systems, components, and styles to create responsive websites and applications.

To adjust the size of elements in Bootstrap, you can use the dimension class, which includes: adjusting width: .col-, .w-, .mw-adjust height: .h-, .min-h-, .max-h-

How to use the Bootstrap button? Introduce Bootstrap CSS to create button elements and add Bootstrap button class to add button text

There are several ways to insert images in Bootstrap: insert images directly, using the HTML img tag. With the Bootstrap image component, you can provide responsive images and more styles. Set the image size, use the img-fluid class to make the image adaptable. Set the border, using the img-bordered class. Set the rounded corners and use the img-rounded class. Set the shadow, use the shadow class. Resize and position the image, using CSS style. Using the background image, use the background-image CSS property.
