如果你的静态资源文件需要 (Vite 或 webpack) 做构建编译处理,可以放到 assets 目录,否则可以放到 public 目录中去。

public

public 目录直接在服务器根目录下提供静态资源, 在 nuxt 2 中叫做 static。

public 目录中的静态资源是公开可用的。可以直接通过 https://[网址].com/images/logo.png 的方式访问 public 目录中的文件、图片等

在应用程序的代码中,使用 public 目录下的文件:

  1. <template>
  2. <img src="/images/logo.png" alt="XXY" />
  3. </template>

assets

需要构建工具(Vite 或 webpack)处理的静态资源,如 css 样式、字体、不会从 public 提供的图形等。

图片

在应用程序的代码中,通过使用 ~/assets/ 或者 @/assets/ 路径来读取位于assets目录下的文件。

  1. <template>
  2. <img src="~/assets/images/contact/address.png" alt="公司地址" />
  3. </template>

全局样式导入

新建 src/assets/styles 目录,新建 src/assets/styles/global.css 全局样式文件

  1. *,
  2. *::before,
  3. *::after {
  4. box-sizing: border-box;
  5. margin: 0;
  6. padding: 0;
  7. }
  8. html,
  9. body {
  10. font-size: 16px;
  11. line-height: 1.5;
  12. -webkit-font-smoothing: antialiased;
  13. -moz-osx-font-smoothing: grayscale;
  14. font-family: "Source Han Sans CN", "Source Han Sans CN-Regular", sans-serif;
  15. }
  16. ul,
  17. li {
  18. list-style: none;
  19. }
  20. a {
  21. text-decoration: none;
  22. }
  23. .global-content {
  24. max-width: 79rem;
  25. margin: auto;
  26. box-sizing: border-box;
  27. padding: 0 2rem;
  28. }
  29. /* 单行文本换行 */
  30. .global-ellipsis-oneline {
  31. overflow: hidden;
  32. text-overflow: ellipsis;
  33. white-space: nowrap;
  34. }

新建 src/assets/styles/normalize.css 重设浏览器默认样式文件

  1. /*! normalize.css v8.0.1 | MIT License | github.com/necolas/normalize.css */
  2. /* Document
  3. ========================================================================== */
  4. /**
  5. * 1. Correct the line height in all browsers.
  6. * 2. Prevent adjustments of font size after orientation changes in iOS.
  7. */
  8. html {
  9. line-height: 1.15; /* 1 */
  10. -webkit-text-size-adjust: 100%; /* 2 */
  11. }
  12. /* Sections
  13. ========================================================================== */
  14. /**
  15. * Remove the margin in all browsers.
  16. */
  17. body {
  18. margin: 0;
  19. }
  20. /**
  21. * Render the `main` element consistently in IE.
  22. */
  23. main {
  24. display: block;
  25. }
  26. /**
  27. * Correct the font size and margin on `h1` elements within `section` and
  28. * `article` contexts in Chrome, Firefox, and Safari.
  29. */
  30. h1 {
  31. font-size: 2em;
  32. margin: 0.67em 0;
  33. }
  34. /* Grouping content
  35. ========================================================================== */
  36. /**
  37. * 1. Add the correct box sizing in Firefox.
  38. * 2. Show the overflow in Edge and IE.
  39. */
  40. hr {
  41. box-sizing: content-box; /* 1 */
  42. height: 0; /* 1 */
  43. overflow: visible; /* 2 */
  44. }
  45. /**
  46. * 1. Correct the inheritance and scaling of font size in all browsers.
  47. * 2. Correct the odd `em` font sizing in all browsers.
  48. */
  49. pre {
  50. font-family: monospace; /* 1 */
  51. font-size: 1em; /* 2 */
  52. }
  53. /* Text-level semantics
  54. ========================================================================== */
  55. /**
  56. * Remove the gray background on active links in IE 10.
  57. */
  58. a {
  59. background-color: transparent;
  60. }
  61. /**
  62. * 1. Remove the bottom border in Chrome 57-
  63. * 2. Add the correct text decoration in Chrome, Edge, IE, Opera, and Safari.
  64. */
  65. abbr[title] {
  66. border-bottom: none; /* 1 */
  67. text-decoration: underline; /* 2 */
  68. text-decoration: underline dotted; /* 2 */
  69. }
  70. /**
  71. * Add the correct font weight in Chrome, Edge, and Safari.
  72. */
  73. b,
  74. strong {
  75. font-weight: bolder;
  76. }
  77. /**
  78. * 1. Correct the inheritance and scaling of font size in all browsers.
  79. * 2. Correct the odd `em` font sizing in all browsers.
  80. */
  81. code,
  82. kbd,
  83. samp {
  84. font-family: monospace; /* 1 */
  85. font-size: 1em; /* 2 */
  86. }
  87. /**
  88. * Add the correct font size in all browsers.
  89. */
  90. small {
  91. font-size: 80%;
  92. }
  93. /**
  94. * Prevent `sub` and `sup` elements from affecting the line height in
  95. * all browsers.
  96. */
  97. sub,
  98. sup {
  99. font-size: 75%;
  100. line-height: 0;
  101. position: relative;
  102. vertical-align: baseline;
  103. }
  104. sub {
  105. bottom: -0.25em;
  106. }
  107. sup {
  108. top: -0.5em;
  109. }
  110. /* Embedded content
  111. ========================================================================== */
  112. /**
  113. * Remove the border on images inside links in IE 10.
  114. */
  115. img {
  116. border-style: none;
  117. }
  118. /* Forms
  119. ========================================================================== */
  120. /**
  121. * 1. Change the font styles in all browsers.
  122. * 2. Remove the margin in Firefox and Safari.
  123. */
  124. button,
  125. input,
  126. optgroup,
  127. select,
  128. textarea {
  129. font-family: inherit; /* 1 */
  130. font-size: 100%; /* 1 */
  131. line-height: 1.15; /* 1 */
  132. margin: 0; /* 2 */
  133. }
  134. /**
  135. * Show the overflow in IE.
  136. * 1. Show the overflow in Edge.
  137. */
  138. button,
  139. input {
  140. /* 1 */
  141. overflow: visible;
  142. }
  143. /**
  144. * Remove the inheritance of text transform in Edge, Firefox, and IE.
  145. * 1. Remove the inheritance of text transform in Firefox.
  146. */
  147. button,
  148. select {
  149. /* 1 */
  150. text-transform: none;
  151. }
  152. /**
  153. * Correct the inability to style clickable types in iOS and Safari.
  154. */
  155. button,
  156. [type="button"],
  157. [type="reset"],
  158. [type="submit"] {
  159. -webkit-appearance: button;
  160. }
  161. /**
  162. * Remove the inner border and padding in Firefox.
  163. */
  164. button::-moz-focus-inner,
  165. [type="button"]::-moz-focus-inner,
  166. [type="reset"]::-moz-focus-inner,
  167. [type="submit"]::-moz-focus-inner {
  168. border-style: none;
  169. padding: 0;
  170. }
  171. /**
  172. * Restore the focus styles unset by the previous rule.
  173. */
  174. button:-moz-focusring,
  175. [type="button"]:-moz-focusring,
  176. [type="reset"]:-moz-focusring,
  177. [type="submit"]:-moz-focusring {
  178. outline: 1px dotted ButtonText;
  179. }
  180. /**
  181. * Correct the padding in Firefox.
  182. */
  183. fieldset {
  184. padding: 0.35em 0.75em 0.625em;
  185. }
  186. /**
  187. * 1. Correct the text wrapping in Edge and IE.
  188. * 2. Correct the color inheritance from `fieldset` elements in IE.
  189. * 3. Remove the padding so developers are not caught out when they zero out
  190. * `fieldset` elements in all browsers.
  191. */
  192. legend {
  193. box-sizing: border-box; /* 1 */
  194. color: inherit; /* 2 */
  195. display: table; /* 1 */
  196. max-width: 100%; /* 1 */
  197. padding: 0; /* 3 */
  198. white-space: normal; /* 1 */
  199. }
  200. /**
  201. * Add the correct vertical alignment in Chrome, Firefox, and Opera.
  202. */
  203. progress {
  204. vertical-align: baseline;
  205. }
  206. /**
  207. * Remove the default vertical scrollbar in IE 10+.
  208. */
  209. textarea {
  210. overflow: auto;
  211. }
  212. /**
  213. * 1. Add the correct box sizing in IE 10.
  214. * 2. Remove the padding in IE 10.
  215. */
  216. [type="checkbox"],
  217. [type="radio"] {
  218. box-sizing: border-box; /* 1 */
  219. padding: 0; /* 2 */
  220. }
  221. /**
  222. * Correct the cursor style of increment and decrement buttons in Chrome.
  223. */
  224. [type="number"]::-webkit-inner-spin-button,
  225. [type="number"]::-webkit-outer-spin-button {
  226. height: auto;
  227. }
  228. /**
  229. * 1. Correct the odd appearance in Chrome and Safari.
  230. * 2. Correct the outline style in Safari.
  231. */
  232. [type="search"] {
  233. -webkit-appearance: textfield; /* 1 */
  234. outline-offset: -2px; /* 2 */
  235. }
  236. /**
  237. * Remove the inner padding in Chrome and Safari on macOS.
  238. */
  239. [type="search"]::-webkit-search-decoration {
  240. -webkit-appearance: none;
  241. }
  242. /**
  243. * 1. Correct the inability to style clickable types in iOS and Safari.
  244. * 2. Change font properties to `inherit` in Safari.
  245. */
  246. ::-webkit-file-upload-button {
  247. -webkit-appearance: button; /* 1 */
  248. font: inherit; /* 2 */
  249. }
  250. /* Interactive
  251. ========================================================================== */
  252. /*
  253. * Add the correct display in Edge, IE 10+, and Firefox.
  254. */
  255. details {
  256. display: block;
  257. }
  258. /*
  259. * Add the correct display in all browsers.
  260. */
  261. summary {
  262. display: list-item;
  263. }
  264. /* Misc
  265. ========================================================================== */
  266. /**
  267. * Add the correct display in IE 10+.
  268. */
  269. template {
  270. display: none;
  271. }
  272. /**
  273. * Add the correct display in IE 10.
  274. */
  275. [hidden] {
  276. display: none;
  277. }

nuxt.config.ts 配置 全局 css
image.png

css 预处理器 less

  1. pnpm install less --save-dev

将上面的 global.css 改为 global.less

  1. *,
  2. *::before,
  3. *::after {
  4. box-sizing: border-box;
  5. margin: 0;
  6. padding: 0;
  7. }
  8. html,
  9. body {
  10. font-size: 16px;
  11. line-height: 1.5;
  12. -webkit-font-smoothing: antialiased;
  13. -moz-osx-font-smoothing: grayscale;
  14. font-family: "Source Han Sans CN", "Source Han Sans CN-Regular", sans-serif;
  15. }
  16. ul,
  17. li {
  18. list-style: none;
  19. }
  20. a {
  21. text-decoration: none;
  22. }
  23. .global-content {
  24. max-width: 75rem;
  25. margin: auto;
  26. padding: 0;
  27. }
  28. /* 单行文本换行 */
  29. .global-ellipsis-oneline {
  30. overflow: hidden;
  31. text-overflow: ellipsis;
  32. white-space: nowrap;
  33. }

在 nuxt.config.ts 中修改 less 的配置

  1. css: [
  2. '@/assets/styles/global.less',
  3. ],

:::info 注意,如果在 global.less 中声明了 less 变量,在页面中使用变量时会报错变量未声明,全局 less 变量需要单独配置 :::

添加全局 less 变量文件

  1. @primary-color: #1890ff;
  1. vite: {
  2. css: {
  3. preprocessorOptions: {
  4. less: {
  5. additionalData: '@import "assets/styles/variable.less";',
  6. },
  7. },
  8. },
  9. },