美文网首页vbenAdmin
vbenAdmin前端框架,点击【项目配置】的【重置】按钮,功能

vbenAdmin前端框架,点击【项目配置】的【重置】按钮,功能

作者: 意随风起 | 来源:发表于2022-07-01 20:39 被阅读0次
    1. 在项目根目录下,文件路径为【/src/settings/projectSetting.ts】,修改【setting】常量里面的【menuSetting.theme】【ThemeEnum.LIGHT】,如下:
    // 此处省略n行代码
    // ! You need to clear the browser cache after the change
    const setting: ProjectConfig = {
      // 此处省略n行代码
      // Menu configuration
      menuSetting: {
        // 此处省略n行代码
        // Menu theme
        theme: ThemeEnum.LIGHT,
        // 此处省略n行代码
      },
      // 此处省略n行代码
    };
    
    export default setting;
    
    
    1. 在项目根目录下,文件路径为【/src/utils/index.ts】,添加【深拷贝】公共方法,如下:
    //深克隆 可传入对象 或 数组
    export function deepClone(obj: any) {
      if (obj === null || !obj) return obj;
      if (Object.prototype.toString.call(obj) === '[object Object]') {
        let target = {};
        const keys = Object.keys(obj);
        keys.forEach((key) => {
          if (obj[key] && typeof obj[key] === 'object') target[key] = deepClone(obj[key]);
          else target[key] = obj[key];
        });
        return target;
      } else if (Array.isArray(obj)) {
        let arr: any[] = [];
        obj.forEach((item, index) => {
          if (item && typeof item === 'object') arr[index] = deepClone(item);
          else arr[index] = item;
        });
        return arr;
      }
    }
    
    1. 在项目根目录下,文件路径为【/src/layouts/default/setting/components/SettingFooter.vue】,引入【更新主题背景色】方法,引入【深拷贝】公共方法,并在点击【重置】按钮后设置其方法,如下:
    <template>
      <div :class="prefixCls">
        <a-button type="primary" block @click="handleCopy">
          <CopyOutlined class="mr-2" />
          {{ t('layout.setting.copyBtn') }}
        </a-button>
    
        <a-button color="warning" block @click="handleResetSetting" class="my-3">
          <RedoOutlined class="mr-2" />
          {{ t('common.resetText') }}
        </a-button>
    
        <a-button color="error" block @click="handleClearAndRedo">
          <RedoOutlined class="mr-2" />
          {{ t('layout.setting.clearBtn') }}
        </a-button>
      </div>
    </template>
    <script lang="ts">
      import { defineComponent, unref } from 'vue';
    
      import { CopyOutlined, RedoOutlined } from '@ant-design/icons-vue';
    
      import { useAppStore } from '/@/store/modules/app';
      import { usePermissionStore } from '/@/store/modules/permission';
      import { useMultipleTabStore } from '/@/store/modules/multipleTab';
      import { useUserStore } from '/@/store/modules/user';
    
      import { useDesign } from '/@/hooks/web/useDesign';
      import { useI18n } from '/@/hooks/web/useI18n';
      import { useMessage } from '/@/hooks/web/useMessage';
      import { useCopyToClipboard } from '/@/hooks/web/useCopyToClipboard';
    
      import { updateColorWeak } from '/@/logics/theme/updateColorWeak';
      import { updateGrayMode } from '/@/logics/theme/updateGrayMode';
      import { updateHeaderBgColor, updateSidebarBgColor } from '/@/logics/theme/updateBackground';
      import { updateDarkTheme } from '/@/logics/theme/dark';
      import { changeTheme } from '/@/logics/theme';
      import { deepClone } from '/@/utils';
      import projectSetting from '/@/settings/projectSetting';
    
      export default defineComponent({
        name: 'SettingFooter',
        components: { CopyOutlined, RedoOutlined },
        setup() {
          const defaultSetting = deepClone(projectSetting)
          const permissionStore = usePermissionStore();
          const { prefixCls } = useDesign('setting-footer');
          const { t } = useI18n();
          const { createSuccessModal, createMessage } = useMessage();
          const tabStore = useMultipleTabStore();
          const userStore = useUserStore();
          const appStore = useAppStore();
    
          function handleCopy() {
            const { isSuccessRef } = useCopyToClipboard(
              JSON.stringify(unref(appStore.getProjectConfig), null, 2),
            );
            unref(isSuccessRef) &&
              createSuccessModal({
                title: t('layout.setting.operatingTitle'),
                content: t('layout.setting.operatingContent'),
              });
          }
          function handleResetSetting() {
            try {
              const { colorWeak, grayMode, headerSetting, menuSetting, themeColor } = defaultSetting;
              appStore.setDarkMode(menuSetting.theme);
              appStore.setProjectConfig(defaultSetting);
              // updateTheme(themeColor);
              updateColorWeak(colorWeak);
              updateGrayMode(grayMode);
              updateDarkTheme(menuSetting.theme);
              updateHeaderBgColor(headerSetting.bgColor);
              updateSidebarBgColor(menuSetting.bgColor);
              changeTheme(themeColor);
              createMessage.success(t('layout.setting.resetSuccess'));
            } catch (error: any) {
              createMessage.error(error);
            }
          }
    
          function handleClearAndRedo() {
            localStorage.clear();
            appStore.resetAllState();
            permissionStore.resetState();
            tabStore.resetState();
            userStore.resetState();
            location.reload();
          }
          return {
            prefixCls,
            t,
            handleCopy,
            handleResetSetting,
            handleClearAndRedo,
          };
        },
      });
    </script>
    <style lang="less" scoped>
      @prefix-cls: ~'@{namespace}-setting-footer';
    
      .@{prefix-cls} {
        display: flex;
        flex-direction: column;
        align-items: center;
      }
    </style>
    
    

    相关文章

      网友评论

        本文标题:vbenAdmin前端框架,点击【项目配置】的【重置】按钮,功能

        本文链接:https://www.haomeiwen.com/subject/udvkbrtx.html