# vue3+node+mysql搭建blog(二)

# 使用BEM样式规范

# 安装scss

npm install node-sass sass-loader --save-dev
1

# BEM规范封装

# 1. src文件夹下创建mixins文件夹和index.scss文件

# 2. mixins文件夹下创建 function.scss 和 mixin.scss文件

src/scss/mixins/function.scss

$namespace: 'my';
$element-separator: '__';
$modifier-separator: '--';
$state-prefix: 'is-';

/* BEM support Func
 -------------------------- */
@function selectorToString($selector) {
  $selector: inspect($selector);
  $selector: str-slice($selector, 2, -2);

  @return $selector;
}

@function containsModifier($selector) {
  $selector: selectorToString($selector);

  @if str-index($selector, $modifier-separator) {
    @return true;
  }
  
  @else {
    @return false;
  }
}

@function containWhenFlag($selector) {
  $selector: selectorToString($selector);

  @if str-index($selector, '.' + $state-prefix) {
    @return true;
  }
  
  @else {
    @return false;
  }
}

@function containPseudoClass($selector) {
  $selector: selectorToString($selector);

  @if str-index($selector, ':') {
    @return true;
  }

  @else {
    @return false;
  }
}

@function hitAllSpecialNestRule($selector) {
  @return containsModifier($selector) or containWhenFlag($selector) or containPseudoClass($selector);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53

src/scss/mixins/mixin.scss

@import './function';

/* Placeholder
 -------------------------- */
@mixin placeholder {
  &::-webkit-input-placeholder {
    @content;
  }

  &::-moz-placeholder {
    @content;
  }

  &:-ms-input-placeholder {
    @content;
  }
}

/* BEM
 *  -   中划线 :仅作为连字符使用,表示某个块或者某个子元素的多单词之间的连接记号。
 *  __  双下划线:双下划线用来连接块和块的子元素
 *  --   双横线:双横线用来描述一个块或者块的子元素的一种状态
 -------------------------- */

// 块(block
@mixin b($block) {
  $b: $namespace+'-'+$block !global;

  .#{$b} {
    @content;
  }
}

// 元素(element)
@mixin e($element) {
  $e: $element !global;
  $selector: &;
  $current-selector: "";

  @each $unit in $element {
    $current-selector: #{$current-selector + "." + $b + $element-separator + $unit + ","};
  }

  @if hitAllSpecialNestRule($selector) {
    @at-root {
      #{$selector} {
        #{$current-selector} {
          @content;
        }
      }
    }
  }

  @else {
    @at-root {
      #{$current-selector} {
        @content;
      }
    }
  }
}

// 修饰符(modifier)
@mixin m($modifier) {
  $selector: &;
  $current-selector: "";

  @each $unit in $modifier {
    $current-selector: #{$current-selector + & + $modifier-separator + $unit + ","};
  }

  @at-root {
    #{$current-selector} {
      @content;
    }
  }
}

@mixin configurable-m($modifier, $E-flag: false) {
  $selector: &;
  $interpolation: '';

  @if $E-flag {
    $interpolation: $element-separator + $E-flag;
  }

  @at-root {
    #{$selector} {
      .#{$b + $interpolation + $modifier-separator + $modifier} {
        @content;
      }
    }
  }
}

@mixin spec-selector($specSelector: '', $element: $E, $modifier: false, $block: $B) {
  $modifier-combo: '';

  @if $modifier {
    $modifier-combo: $modifier-separator + $modifier;
  }

  @at-root {
    #{&}#{$specSelector}.#{$block + $element-separator + $element + $modifier-combo} {
      @content;
    }
  }
}

@mixin meb($modifier: false, $element: $E, $block: $B) {
  $selector: &;
  $modifier-combo: '';

  @if $modifier {
    $modifier-combo: $modifier-separator + $modifier;
  }

  @at-root {
    #{$selector} {
      .#{$block + $element-separator + $element + $modifier-combo} {
        @content;
      }
    }
  }
}

@mixin when($state) {
  @at-root {
    &.#{$state-prefix + $state} {
      @content;
    }
  }
}

@mixin extend-rule($name) {
  @extend #{'%shared-'+$name};
}

@mixin share-rule($name) {
  $rule-name: '%shared-'+$name;

  @at-root #{$rule-name} {
    @content;
  }
}

@mixin pseudo($pseudo) {
  @at-root #{&}#{':#{$pseudo}'} {
    @content;
  }
}

/* 通用 图片处理 $img 指路径资源
 -------------------------- */
@mixin base-icon($img, $width:16px, $height:16px) {
  display: inline-block;
  height: $height;
  position: relative;
  vertical-align: middle;
  width: $width;

  &::before {
    background: url("#{$icon-img-url}#{$img}") no-repeat;
    background-size: 100%;
    content: "";
    display: block;
    height: $height;
    position: absolute;
    top: 0;
    width: $width;

    @content;
  }
}

/* 1px 解决方案
 -------------------------- */
@mixin border-1px($color: #ccc, $radius: 2px, $style: solid) {
  position: relative;
  
  &::after {
    border: 1RPX $style $color;
    border-radius: $radius;
    box-sizing: border-box;
    content: "";
    display: block;
    height: 100%;
    left: 0;
    pointer-events: none;
    position: absolute;
    top: 0;
    transform-origin: 0 0;
    width: 100%;
    
    @content;

    @media (min-resolution: 2dppx) {
      border-radius: $radius * 2;
      height: 200%;
      transform: scale(0.5) translateZ(0);
      width: 200%;
    }

    @media (min-resolution: 3dppx) {
      border-radius: $radius * 3;
      height: 300%;
      transform: scale(0.333) translateZ(0);
      width: 300%;
    }
  }
}

@mixin ext-overflow($t) {
  -webkit-box-orient: vertical;
  -webkit-box-pack: center;
  display: -webkit-box;
  -webkit-line-clamp: $t;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: normal;
  word-break: break-all;
}

@mixin text-overflow-1 {
  display: block;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

// 解决 320 的 兼容性问题
@mixin mini320 {
  @media screen and (min-width: 318px) and (max-width: 360px) {
    @content;
  }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236

src/scss/index.scss

@import "./mixins/mixin.scss";
1

# 3. vue.config.js 配置全局引入样式

module.exports = {
  runtimeCompiler: true,
  // 代理配置
  devServer: {
    proxy: {
      '/api': {
        target: 'http://localhost:7000',
        changeOrigin: true,
        pathRewrite: {
          '^/api': ''
        }
      }
    }
  },
  // css全局引入样式
  css: {
    loaderOptions: {
      sass: {
        data: `@import "@/scss/index.scss";`
      }
    }
  }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23