Node.js/nuxt/css module:修订间差异

来自康健生活
Kangkk​(留言 | 贡献)
Kangkk​(留言 | 贡献)
 
(未显示同一用户的5个中间版本)
第1行: 第1行:
== 引言 ==
[https://github.com/css-modules/css-modules css-modules]
== 配置 ==
== 配置 ==


配置文件
配置文件,网上众说纷纭,把我看得笑哭了,配置文件要两步走,一是看官方文档(其实内容很片面),而是看官方示例([https://github.com/nuxt/nuxt.js/blob/19e68e770c1d50a97cef7743e93e4e8c6aaed80c/packages/config/src/config/build.js 示例一]、[https://github.com/nuxt/nuxt.js/blob/19e68e770c1d50a97cef7743e93e4e8c6aaed80c/packages/config/test/config/__snapshots__/index.test.js.snap 示例二])
{{hc|nuxt.config.js|<syntaxhighlight lang="javascript">
 
build: {
  css: {
    loaderOptions: {
      css: {
        localIdentName: '[name]__[local]-[hash:base64:5]',
        camelCase: true
      }
    }
  },
}</syntaxhighlight>}}
或者
{{hc|nuxt.config.js|<syntaxhighlight lang="javascript">
{{hc|nuxt.config.js|<syntaxhighlight lang="javascript">
{
/*!
   loaders:{
* nuxt 配置文件
    cssModules: {
* @auther kangkk
      localIdentName: '[local]_[hash:base64:5]',
*/
       modules: true,
...
      camelCase: true
const config = {
   mode: 'universal',
...
  /*
  ** Build configuration
  */
  build: {
    loaders: {
      cssModules: {
        modules: {
          localIdentName: "[path][name]-[local]-[hash:5]"
        }
       },
     },
     },
     sass: {
     ...
      indentedSyntax: true
}
    },
 
  },
export default config}</syntaxhighlight>}}
}</syntaxhighlight>}}
 
在配置文件的[https://zh.nuxtjs.org/api/configuration-build/#loaderscss-and-loaderscssmodules 说明文档]中标注了:
 
{{Warning|cssModules 是使用的 loader 选项 [https://vue-loader.vuejs.org/guide/css-modules.html#usage CSS Modules]}}
 
== 使用方法 ==
== 使用方法 ==
=== style ===
=== style ===
<syntaxhighlight lang="html5">
<syntaxhighlight lang="html5">
<template>
<template>
   <div :class="$style.authentication">
   <div :class="$header.authentication">
     <!-- logo 模块 -->
     <!-- logo 模块 -->
     <div :class="$style.authentication__logo">
     <div :class="$header.authentication__logo">
       <img :class="$style.img"
       <img :class="$header.img"
           src="~assets/img/logo.png"/>
           src="~assets/img/logo.png"/>
     ...
     ...
   </div>
   </div>
</template>
<style module lang="scss">@import "./networkAuthorizationAuthentication";</style></syntaxhighlight>
=== script ===
<syntaxhighlight lang="html5">
<template>
   <button :class="{
   <button :class="{
    'global-button-class-name': true,
     [$header.button]: true,
     [styles.button]: true,
    [styles.mini]: mini
   }">登录</button>
   }">登录</button>
</template>
</template>
<style module="header" lang="scss">@import "./networkAuthorizationAuthentication";</style>
<script>
<script>
  import styles from './button.css'
   export default {
   export default {
    props: { mini: Boolean },
     data: () => (
     data: () => ({ styles })
      console.log(this.$style)
      rturn { list }
    )
   }
   }
</script></syntaxhighlight>
</script></syntaxhighlight>
 
=== module ===
不足:
* 在 data 中传入 styles
* 使用 styles.localClassName 导入全局类名
* 和组件的属性值绑定
==== vue-css-modules ====
<syntaxhighlight lang="html5">
<syntaxhighlight lang="html5">
<template>
<template>
   <button
   <!-- header start-->
    class="global-button-class-name"
  <el-header :class="{ [headerStyle.header]: true }">
    styleName="button :mini">
     <div class="header-body">
     登录
      ...
  </button>
</template>
</template>
<style module="headerStyle" lang="scss">@import "./header.scss";</style></syntaxhighlight>
<script>
  import CSSModules from 'vue-css-modules'
  import styles from './button.css'
  export default {
    mixins: [CSSModules(styles)],
    props: { mini: Boolean }
  }
</script></syntaxhighlight>