参考:https://blog.csdn.net/qq_41387882/article/details/106294485
Nuxt配置全局less
1. 配置全局less
使用less先安装less less-loader,nuxt会自动识别导入的后缀名,在nuxt.config.js中配置
css: [
/**
* 配置全局 css
*/
'element-ui/lib/theme-chalk/index.css',
'@assets/styles/global.less',
],
2.配置全局 scss 变量
为页面注入变量可以使用@nuxtjs/style-resources 来实现安装yarn add @nuxtjs/style-resources -D配置nuxt.config.js
modules: [
// Doc: https://axios.nuxtjs.org/usage
'@nuxtjs/axios',
'@nuxtjs/style-resources'
],
styleResources:{
less:[
'./assets/styles/variable.less'
]
},
styleResources 配置的资源路径不能使用 ~ 和 @,要使用绝对或者相对路径
nuxt.config参考配置
const path = require('path')
import lessToJs from 'less-vars-to-js';
import { resolve } from 'path'
import fs from 'fs';
const globalLess = fs.readFileSync('./static/css/theme.less', 'utf8'); // 读取文件
const theme = lessToJs(globalLess) // less文件转换为JS文件
console.log(process.env.VUE_APP_TITLE, '======== nuxt.config.js ======');
const plugins = [
["@babel/plugin-proposal-private-methods", { "loose": true }]
]
if (process.env.VUE_APP_TITLE === 'production') {
plugins.push("transform-remove-console")
}
// todo 配置多环境参数
const config = {
production: {
'tradeCenterUrl': 'http://IP地址:端口',
'rzlineUrl': 'http://IP地址:端口',
'ws': 'wss://地址',
'oss': { // OSS配置
"region": "",
"accessKeyId": "",
"accessKeySecret": "",
"bucket": "",
"endpoint": "",
"cname": true
}
},
test: {
'tradeCenterUrl': 'http://IP地址:端口',
'rzlineUrl': 'http://IP地址:端口',
'ws': 'wss://地址',
'oss': { // OSS配置
"region": "",
"accessKeyId": "",
"accessKeySecret": "",
"bucket": "",
"endpoint": "",
"cname": true
}
},
development: {
'tradeCenterUrl': 'http://IP地址:端口',
'rzlineUrl': 'http://IP地址:端口',
'oss': { // OSS配置
"region": "",
"accessKeyId": "",
"accessKeySecret": "",
"bucket": "",
"endpoint": "",
"cname": true
}
}
}
export default {
env: {
VUE_APP_TITLE: process.env.VUE_APP_TITLE,
baseURL: '/api',
config
},
// Global page headers: https://go.nuxtjs.dev/config-head
head: {
title: 'title',
htmlAttrs: {
lang: 'en'
},
meta: [
{ charset: 'utf-8' },
{ name: 'viewport', content: 'width=device-width, initial-scale=1' },
// { hid: 'description', name: 'description', content: '' }
],
link: [
{ rel: 'icon', type: 'image/x-icon', href: '/favicon.ico' }
]
},
router: {
// 加上base: '/'会导致svg的图标刷新页面不展示
// base: '/'
},
// 设置别名
alias: {
'@a': resolve(__dirname, './assets'),
'@c': resolve(__dirname, './components'),
'@p': resolve(__dirname, './pages')
},
// Global CSS: https://go.nuxtjs.dev/config-css
css: [
'ant-design-vue/dist/antd.less' // UI框架的CSS引入
],
// Plugins to run before rendering page: https://go.nuxtjs.dev/config-plugins
plugins: [
'@/plugins/antd-ui', // UI框架
"@/plugins/axios_interceptors", // axios的配置
"@/plugins/api/api", // 全局api的维护
"@/plugins/my-prototype.js", // 在client和service上生效
{ src: '~/plugins/my-plugins.client.js', mode: 'client' }, // 只在client上生效
{ src: '~/plugins/dict.js' }, // 数据字典
{ src: "~/plugins/vue-swiper.js", ssr: false }, // 滚动动画
{ src: '@/plugins/icons', ssr: true }, // svg图片引入
'~/plugins/lodash', // 请求延缓
],
// 中间件
serverMiddleware: [
{ path: '/middlewar', handler: '@/server-middleware/middlewar.js' }
],
// Auto import components: https://go.nuxtjs.dev/config-components
components: true,
// Modules for dev and build (recommended): https://go.nuxtjs.dev/config-modules
buildModules: [
],
// Modules: https://go.nuxtjs.dev/config-modules
modules: [
'@nuxtjs/axios', // 为了使用axios进行请求
'@nuxtjs/style-resources', // 为了处理less的文件
'cookie-universal-nuxt', // 为了将store数据缓存到cookie中,进行vuex数据的缓存(不适宜配置多个数据,因为cookie的数据是会传输到后台的)
'vue-social-sharing/nuxt' // 分享
],
// 引入公共的less
styleResources: {
less: [
'./assets/css/global.less',
]
},
axios: {
// 配置请求基础路径
// baseURL: 'http://localhost:80',
proxy: true,
// 配置失败之后重复请求的次数retries
retry: { retries: 1 },
headers: {
common: {},
post: {
'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
}
}
},
// 设置代理
proxy: {
"/api": {
target: config[process.env.VUE_APP_TITLE].url,
ws: true, // 代理websockets
changeOrigin: true, // 是否跨域,虚拟的站点需要更管origin
pathRewrite: {
'^/api': '/'
}
},
},
// Build Configuration: https://go.nuxtjs.dev/config-build
build: {
// 解决 启动 报Though the “loose“ option was set to “false“ in your @babel/preset-env config, it will not ...
babel: {
plugins
},
loaders: {
less: {
// 配置全局的颜色(更改ant-design的主题色)
modifyVars: theme,
javascriptEnabled: true
}
},
extend (config, ctx) {
// todo svg (svg配置)
const svgRule = config.module.rules.find(rule => rule.test.test('.svg'))
svgRule.exclude = [path.resolve(__dirname, 'icons/svg')]
// Includes /icons/svg for svg-sprite-loader
config.module.rules.push({
test: /\.svg$/,
include: [path.resolve(__dirname, 'icons/svg')],
loader: 'svg-sprite-loader',
options: {
symbolId: 'icon-[name]'
}
})
}
},
// 启动端口号
server: {
port: 3001, // default: 3000
host: '0.0.0.0'
}
}