This time I will show you how Vue implements local scope & modularization, and what are the precautions for Vue to implement local scope & modularization. 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 */ .button__button--d8fj3 { font-size: 16px; } .button__mini--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: 'button__button--d8fj3', // mini: 'button__mini--f90jc' // } element.innerHTML = '<button class="' + styles.button + ' ' + styles.mini + '" />'
vue-css-modules: Simplified class name mapping
The following is a button component using CSS Modules:
<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
<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>
Modifier
@button<button styleName="@button">按钮</button>
<button styleName="button" data-component-button="true">按钮</button>
.form [data-component-button] { font-size: 20px; }
<button styleName="$type">按钮</button>
<button :styleName="type">按钮</button>
<button styleName=":mini">按钮</button>
<button :styleName="mini ? 'mini' : ''">按钮</button> disabled=isDisabled <button styleName="disabled=isDisabled">按钮</button>
<button :styleName="isDisabled ? 'disabled' : ''">按钮</button>
Usage method
Use in the Vue templateIntroduce 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>
<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>
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> ) } }
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' }, '点我') } }
Use JS to operate pictures, leaving only black and white
Encapsulate the Vue2 routing navigation hook and use it in actual combat use
The above is the detailed content of How Vue implements local scope & modularization. For more information, please follow other related articles on the PHP Chinese website!