vue3 element plus 更换主题颜色

实测有效

1.src下新建文件styles/element/index.scss

// styles/element/index.scss
/* 只需要重写你需要的即可 */
 
@forward 'element-plus/theme-chalk/src/common/var.scss' with (
  $colors: (
    'primary': (
      'base': #b78e81,
    ),
    'success': (
      'base': #b78e81,
    ),
  ),
);

2.vite.config.ts 里面新增依赖

// 新增 npm i unplugin-vue-components
// 新增  npm i unplugin-auto-import

import AutoImport from "unplugin-auto-import/vite";
import Components from "unplugin-vue-components/vite";
import { ElementPlusResolver } from "unplugin-vue-components/resolvers";

3.配置vite.config.ts 重新启动即可

import { defineConfig } from "vite";
import vue from "@vitejs/plugin-vue";
import { resolve } from "path";

import AutoImport from "unplugin-auto-import/vite";
import Components from "unplugin-vue-components/vite";
import { ElementPlusResolver } from "unplugin-vue-components/resolvers";

// https://vitejs.dev/config/
const pathResolve = (dir: string) => {
  return resolve(__dirname, ".", dir);
};
const alias: Record = {
  "/@": pathResolve("./src/"),
};


// vite.config
export default defineConfig({
  plugins: [
    vue(),
    // 1.第一步
    AutoImport({
      resolvers: [ElementPlusResolver()],
    }),
    // 2.第二步
    Components({
      //1.配置elementPlus采用css样式文件
      resolvers: [ElementPlusResolver({ importStyle: "sass" })],
    }),
  ],
  resolve: { alias },
  css: {
    preprocessorOptions: {
    // 3.第三步
      scss: {
        // additionalData: `@import "./src/style/mixin.scss";`, // 此处全局的scss文件
        additionalData: `
          @use "/@/styles/element/index.scss" as *;
        `,
      },
    },
  },
  server: {
    proxy: {
      "/api": {
        target: "http://localhost:3000",
        changeOrigin: true,
      },
    },
  },
});

你可能感兴趣的:(vue.js,前端,javascript)