[CSS Modules](https://github.com/css-modules/css-modules) is a popular system for modularizing and composing CSS. `vue-loader` provides first-class integration with CSS Modules as an alternative for simulated scoped CSS.
@@ -19,9 +19,9 @@ First, CSS Modules must be enabled by passing `modules: true` to `css-loader`:
{
loader:'css-loader',
options:{
// enable CSS Modules
// 开启 CSS Modules
modules:true,
// customize generated class names
// 自定义生成的类名
localIdentName:'[local]_[hash:base64:8]'
}
}
...
...
@@ -32,7 +32,7 @@ First, CSS Modules must be enabled by passing `modules: true` to `css-loader`:
}
```
Then, add the `module` attribute to your `<style>`:
然后在你的 `<style>` 上添加 `module` 特性:
``` vue
<stylemodule>
...
...
@@ -45,7 +45,7 @@ Then, add the `module` attribute to your `<style>`:
</style>
```
The `module` attribute instructs Vue Loader to inject the CSS modules locals object into the component as a computed property with the name `$style`. You can then use it in your templates with a dynamic class binding:
@@ -55,7 +55,7 @@ The `module` attribute instructs Vue Loader to inject the CSS modules locals obj
</template>
```
Since it's a computed property, it also works with the object/array syntax of `:class`:
因为这是一个计算属性,所以它也支持 `:class` 的对象/数组语法:
``` vue
<template>
...
...
@@ -70,7 +70,7 @@ Since it's a computed property, it also works with the object/array syntax of `:
</template>
```
And you can also access it from JavaScript:
你也可以通过 JavaScript 访问到它:
``` vue
<script>
...
...
@@ -78,24 +78,24 @@ export default {
created(){
console.log(this.$style.red)
// -> "red_1VyoJ-uZ"
// an identifier generated based on filename and className.
// 一个基于文件名和类名生成的标识符
}
}
</script>
```
Refer to the [CSS Modules spec](https://github.com/css-modules/css-modules) for mode details such as [global exceptions](https://github.com/css-modules/css-modules#exceptions) and [composition](https://github.com/css-modules/css-modules#composition).
@@ -109,7 +109,7 @@ If you only want to use CSS Modules in some of your Vue components, you can use
}
]
},
// this matches plain <style> or <style scoped>
// 这里匹配普通的 `<style>` 或 `<style scoped>`
{
use:[
'vue-style-loader',
...
...
@@ -120,9 +120,9 @@ If you only want to use CSS Modules in some of your Vue components, you can use
}
```
## Using with Pre-Processors
## 和预处理器配合使用
CSS Modules can be used along with other pre-processors:
CSS Modules 可以独立用于其它预处理器:
``` js
// webpack.config.js -> module.rules
...
...
@@ -139,16 +139,16 @@ CSS Modules can be used along with other pre-processors:
}
```
## Custom Inject Name
## 自定义的注入名称
You can have more than one `<style>` tags in a single `*.vue` component. To avoid injected styles to overwrite each other, you can customize the name of the injected computed property by giving the `module` attribute a value: