参考:https://blog.csdn.net/qq_41387882/article/details/106294485

Nuxt配置全局less

https://blog.csdn.net/my466879168/article/details/105990174/?utm_term=lessnuxt%E5%85%A8%E5%B1%80&utm_medium=distribute.pc_aggpage_search_result.none-task-blog-2~all~sobaiduweb~default-0-105990174&spm=3001.4430

1. 配置全局less

使用less先安装less less-loader,nuxt会自动识别导入的后缀名,在nuxt.config.js中配置

  1. css: [
  2. /**
  3. * 配置全局 css
  4. */
  5. 'element-ui/lib/theme-chalk/index.css',
  6. '@assets/styles/global.less',
  7. ],

2.配置全局 scss 变量

为页面注入变量可以使用@nuxtjs/style-resources 来实现安装yarn add @nuxtjs/style-resources -D配置nuxt.config.js

  1. modules: [
  2. // Doc: https://axios.nuxtjs.org/usage
  3. '@nuxtjs/axios',
  4. '@nuxtjs/style-resources'
  5. ],
  6. styleResources:{
  7. less:[
  8. './assets/styles/variable.less'
  9. ]
  10. },

styleResources 配置的资源路径不能使用 ~ 和 @,要使用绝对或者相对路径

nuxt.config参考配置

  1. const path = require('path')
  2. import lessToJs from 'less-vars-to-js';
  3. import { resolve } from 'path'
  4. import fs from 'fs';
  5. const globalLess = fs.readFileSync('./static/css/theme.less', 'utf8'); // 读取文件
  6. const theme = lessToJs(globalLess) // less文件转换为JS文件
  7. console.log(process.env.VUE_APP_TITLE, '======== nuxt.config.js ======');
  8. const plugins = [
  9. ["@babel/plugin-proposal-private-methods", { "loose": true }]
  10. ]
  11. if (process.env.VUE_APP_TITLE === 'production') {
  12. plugins.push("transform-remove-console")
  13. }
  14. // todo 配置多环境参数
  15. const config = {
  16. production: {
  17. 'tradeCenterUrl': 'http://IP地址:端口',
  18. 'rzlineUrl': 'http://IP地址:端口',
  19. 'ws': 'wss://地址',
  20. 'oss': { // OSS配置
  21. "region": "",
  22. "accessKeyId": "",
  23. "accessKeySecret": "",
  24. "bucket": "",
  25. "endpoint": "",
  26. "cname": true
  27. }
  28. },
  29. test: {
  30. 'tradeCenterUrl': 'http://IP地址:端口',
  31. 'rzlineUrl': 'http://IP地址:端口',
  32. 'ws': 'wss://地址',
  33. 'oss': { // OSS配置
  34. "region": "",
  35. "accessKeyId": "",
  36. "accessKeySecret": "",
  37. "bucket": "",
  38. "endpoint": "",
  39. "cname": true
  40. }
  41. },
  42. development: {
  43. 'tradeCenterUrl': 'http://IP地址:端口',
  44. 'rzlineUrl': 'http://IP地址:端口',
  45. 'oss': { // OSS配置
  46. "region": "",
  47. "accessKeyId": "",
  48. "accessKeySecret": "",
  49. "bucket": "",
  50. "endpoint": "",
  51. "cname": true
  52. }
  53. }
  54. }
  55. export default {
  56. env: {
  57. VUE_APP_TITLE: process.env.VUE_APP_TITLE,
  58. baseURL: '/api',
  59. config
  60. },
  61. // Global page headers: https://go.nuxtjs.dev/config-head
  62. head: {
  63. title: 'title',
  64. htmlAttrs: {
  65. lang: 'en'
  66. },
  67. meta: [
  68. { charset: 'utf-8' },
  69. { name: 'viewport', content: 'width=device-width, initial-scale=1' },
  70. // { hid: 'description', name: 'description', content: '' }
  71. ],
  72. link: [
  73. { rel: 'icon', type: 'image/x-icon', href: '/favicon.ico' }
  74. ]
  75. },
  76. router: {
  77. // 加上base: '/'会导致svg的图标刷新页面不展示
  78. // base: '/'
  79. },
  80. // 设置别名
  81. alias: {
  82. '@a': resolve(__dirname, './assets'),
  83. '@c': resolve(__dirname, './components'),
  84. '@p': resolve(__dirname, './pages')
  85. },
  86. // Global CSS: https://go.nuxtjs.dev/config-css
  87. css: [
  88. 'ant-design-vue/dist/antd.less' // UI框架的CSS引入
  89. ],
  90. // Plugins to run before rendering page: https://go.nuxtjs.dev/config-plugins
  91. plugins: [
  92. '@/plugins/antd-ui', // UI框架
  93. "@/plugins/axios_interceptors", // axios的配置
  94. "@/plugins/api/api", // 全局api的维护
  95. "@/plugins/my-prototype.js", // 在client和service上生效
  96. { src: '~/plugins/my-plugins.client.js', mode: 'client' }, // 只在client上生效
  97. { src: '~/plugins/dict.js' }, // 数据字典
  98. { src: "~/plugins/vue-swiper.js", ssr: false }, // 滚动动画
  99. { src: '@/plugins/icons', ssr: true }, // svg图片引入
  100. '~/plugins/lodash', // 请求延缓
  101. ],
  102. // 中间件
  103. serverMiddleware: [
  104. { path: '/middlewar', handler: '@/server-middleware/middlewar.js' }
  105. ],
  106. // Auto import components: https://go.nuxtjs.dev/config-components
  107. components: true,
  108. // Modules for dev and build (recommended): https://go.nuxtjs.dev/config-modules
  109. buildModules: [
  110. ],
  111. // Modules: https://go.nuxtjs.dev/config-modules
  112. modules: [
  113. '@nuxtjs/axios', // 为了使用axios进行请求
  114. '@nuxtjs/style-resources', // 为了处理less的文件
  115. 'cookie-universal-nuxt', // 为了将store数据缓存到cookie中,进行vuex数据的缓存(不适宜配置多个数据,因为cookie的数据是会传输到后台的)
  116. 'vue-social-sharing/nuxt' // 分享
  117. ],
  118. // 引入公共的less
  119. styleResources: {
  120. less: [
  121. './assets/css/global.less',
  122. ]
  123. },
  124. axios: {
  125. // 配置请求基础路径
  126. // baseURL: 'http://localhost:80',
  127. proxy: true,
  128. // 配置失败之后重复请求的次数retries
  129. retry: { retries: 1 },
  130. headers: {
  131. common: {},
  132. post: {
  133. 'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
  134. }
  135. }
  136. },
  137. // 设置代理
  138. proxy: {
  139. "/api": {
  140. target: config[process.env.VUE_APP_TITLE].url,
  141. ws: true, // 代理websockets
  142. changeOrigin: true, // 是否跨域,虚拟的站点需要更管origin
  143. pathRewrite: {
  144. '^/api': '/'
  145. }
  146. },
  147. },
  148. // Build Configuration: https://go.nuxtjs.dev/config-build
  149. build: {
  150. // 解决 启动 报Though the “loose“ option was set to “false“ in your @babel/preset-env config, it will not ...
  151. babel: {
  152. plugins
  153. },
  154. loaders: {
  155. less: {
  156. // 配置全局的颜色(更改ant-design的主题色)
  157. modifyVars: theme,
  158. javascriptEnabled: true
  159. }
  160. },
  161. extend (config, ctx) {
  162. // todo svg (svg配置)
  163. const svgRule = config.module.rules.find(rule => rule.test.test('.svg'))
  164. svgRule.exclude = [path.resolve(__dirname, 'icons/svg')]
  165. // Includes /icons/svg for svg-sprite-loader
  166. config.module.rules.push({
  167. test: /\.svg$/,
  168. include: [path.resolve(__dirname, 'icons/svg')],
  169. loader: 'svg-sprite-loader',
  170. options: {
  171. symbolId: 'icon-[name]'
  172. }
  173. })
  174. }
  175. },
  176. // 启动端口号
  177. server: {
  178. port: 3001, // default: 3000
  179. host: '0.0.0.0'
  180. }
  181. }