Vben Admin Documentation

repository·main·Indexed 18 days ago

https://github.com/vbenjs/vue-vben-admin-doc

Technical references and guides for the Vben Admin project. Includes detailed documentation for a variety of Vue components such as Authority for permission management, CodeEditor for syntax-highlighted editing, CropperImage for image cropping, and utility components like ClickOutSide, CountTo, and BasicHelp.

Tokens
61.3K
Snippets
198
Records
258
Agent score
63%

What's inside vue-vben-admin-doc

  1. Overview of Vue-Vben-Admin

    main

    Vue-Vben-Admin is a professional backend solution built on Vue 3.0, Vite, Ant Design Vue, and TypeScript. It is designed for medium-to-large scale projects and provides out-of-the-box features including:

    • Second-layer encapsulated components
    • Utils and Hooks
    • Dynamic menus
    • Permission validation
    • Button-level permission control

    It serves as both a project starter template for enterprise-level products and a learning resource for modern frontend technologies.

  2. Find related Vben integration projects

    main

    The following list contains community-driven (non-official) projects that integrate with Vben across various backend technologies. These projects are open-sourced by Vben users. Please check the specific license for each repository before use.

    Golang

    PHP

    Java

    .NET

  3. Understand the three permission modes

    main

    The project supports three distinct methods for handling permissions:

    1. Frontend Role-based (ROLE mode): Menus are filtered on the frontend based on user roles. Routes and menus are configured separately. This is suitable for systems with relatively fixed roles.
    2. Frontend Role-based (Automatic): Menus are automatically generated from the route configuration, also filtered by user roles on the frontend.
    3. Backend Dynamic (BACK mode): The route table is dynamically generated by the backend via an API. The frontend processes this data into a recognizable structure and adds it to the router instance.
  4. Manage language files and directory structure

    main

    Language translation files are located in src/locales/lang/. The project uses a hierarchical directory structure to manage large-scale translations. Files are automatically imported and converted into a multi-level object structure.

    For example, if you have a file at src/locales/lang/zh_CN/components/modal.ts containing:

    {
      title: '标题';
    }

    You can access this translation in your code using the dot notation: t('components.modal.title').

  5. How menu modules work

    main

    A single menu file is treated as a MenuModule. Each module contains an orderNo for sorting and a menu object containing the actual menu configuration.

    Note on Paths: When defining children, the path field should not start with a /. The system automatically resolves child paths by appending them to the parent path.

    Example of a module definition:

    import type { MenuModule } from '/@/router/types';
    import { t } from '/@/hooks/web/useI18n';
    
    const menu: MenuModule = {
      orderNo: 10,
      menu: {
        name: t('routes.dashboard.dashboard'),
        path: '/dashboard',
        children: [
          {
            path: 'analysis',
            name: t('routes.dashboard.analysis'),
          },
          {
            path: 'workbench',
            name: t('routes.dashboard.workbench'),
          },
        ],
      },
    };
    
    export default menu;

    This module is transformed into a flattened structure where child paths become dashboard/analysis and dashboard/workbench.

    import type { MenuModule } from '/@/router/types';
    import { t } from '/@/hooks/web/useI18n';
    const menu: MenuModule = {
      orderNo: 10,
      menu: {
        name: t('routes.dashboard.dashboard'),
        path: '/dashboard',
    
        children: [
          {
            path: 'analysis',
            name: t('routes.dashboard.analysis'),
          },
          {
            path: 'workbench',
            name: t('routes.dashboard.workbench'),
          },
        ],
      },
    };
    export default menu;
  6. Fetch language data from a remote API

    main

    The application waits for setupI18n to complete in src/main.ts before mounting the app. This allows you to perform asynchronous operations (like AJAX requests) to fetch translation data from a server.

    Implementation in setupI18n

    In src/locales/setupI18n.ts, you can modify createI18nOptions to fetch the default language data from an API instead of local files.

    Implementation in changeLocale

    When manually switching languages via changeLocale, the function is designed to be asynchronous. You can intercept the process to fetch the required language module from a remote source before calling globalI18n.setLocaleMessage and moment.updateLocale.

    // src/main.ts
    // The app waits for setupI18n to finish before mounting
    await setupI18n(app);
    
    app.mount('#app', true);
  7. Understand the project's styling architecture

    main

    The project uses Less as the default preprocessor to maintain consistency with Ant Design. Common styles are centralized in the src/design/ directory.

    Directory Structure

    • ant/: Overrides for Ant Design styles.
    • color.less: Color definitions.
    • index.less: Entry point for styles.
    • public.less: Public utility classes.
    • theme.less: Theme-related styles.
    • config.less: Globally injected configuration. This file is automatically available in all components, allowing you to use variables without manual imports.
    • transition/: Animation-related styles.
    • var/: Variable definitions.

    To use variables in a component, simply use them within a <style lang="less"> block; config.less is already implicitly injected.

    <style lang="less" scoped>
      // config.less is implicitly injected here
      .my-class {
        color: @primary-color; // Example variable usage
      }
    </style>
  8. Choose between vue-vben-admin and vue-vben-admin-thin

    main

    Depending on your needs, you can choose between two versions of the template:

    1. vue-vben-admin: The full version containing various Demo examples and integrated plugin usage. Recommended for learning or reference.
    2. vue-vben-admin-thin: A lightweight version. It removes examples, unused files, features, and dependencies to provide a clean slate. You can install specific libraries as needed. Because it uses vite, removing dependencies will not trigger warnings for missing components or hooks.
  9. Use Dynamic Production Configuration via _app.config.js

    main

    When running yarn build, the project generates a _app.config.js file and inserts it into index.html. This file allows you to modify configuration (like API URLs) in the production environment without rebuilding the project.

    How it works

    1. Define variables starting with VITE_GLOB_ in your .env files.
    2. After building, locate /dist/_app.config.js.
    3. Modify the values inside the window.__PRODUCTION__VUE_VBEN_ADMIN__CONF__ object.
    4. Refresh the browser to apply changes.

    Accessing Glob Settings

    To retrieve these dynamic variables in your code, use the useGlobSetting hook provided in src/hooks/setting/index.ts.

    // Example of the generated _app.config.js structure
    window.__PRODUCTION__VUE_VBEN_ADMIN__CONF__ = {
      VITE_GLOB_APP_TITLE: 'vben admin',
      VITE_GLOB_APP_SHORT_NAME: 'vue_vben_admin',
      VITE_GLOB_API_URL: '/app',
      VITE_GLOB_API_URL_PREFIX: '/',
      VITE_GLOB_UPLOAD_URL: '/upload',
    };
  10. Understand project path aliases

    main
    The project uses /@/ as a Vite alias. For example, /@/settings is equivalent to src/settings. This specific naming convention (/@/) was maintained to ensure compatibility during the transition from Vite 1.0, where aliases were required to start with a /.
  11. Understand the project directory structure

    main

    The project follows a structured layout for organized development:

    • build/: Build scripts, configurations, and Vite settings.
    • mock/: Mock data definitions.
    • public/: Static assets.
    • src/: Main source code directory:
      • api/: API interface definitions.
      • assets/: Icons, images, and SVGs.
      • components/: Shared components.
      • design/: Style files.
      • directives/: Custom Vue directives.
      • enums/: Enums and constants.
      • hooks/: Composition API hooks (categorized into component, core, event, setting, and web).
      • layouts/: Layout files (default, iframe, page).
      • locales/: Internationalization (i18n) files.
      • logics/: Business logic.
      • router/: Route configurations.
      • settings/: Project settings (component, design, encryption, locale, project, and site settings).
      • store/: State management (Pinia/Vuex).
      • utils/: Utility functions.
      • views/: Page components.
    • types/: TypeScript type definitions.
    • vite.config.ts: Vite configuration file.
    .
    ├── build
    ├── mock
    ├── public
    ├── src
    │   ├── api
    │   ├── assets
    │   ├── components
    │   ├── design
    │   ├── directives
    │   ├── enums
    │   ├── hooks
    │   ├── layouts
    │   ├── locales
    │   ├── logics
    │   ├── router
    │   ├── settings
    │   ├── store
    │   ├── utils
    │   └── views
    ├── types
    └── vite.config.ts
  12. How component library styles are loaded

    main

    To optimize local development speed, the project does not use on-demand style importing during development. Instead, it performs a full import of ant-design-vue/dist/antd.less to reduce the number of HTTP requests (avoiding ~100 extra requests per page).

    On-demand style importing via vite-plugin-style-import is only activated during the production build process.

    // src/main.ts
    if (import.meta.env.DEV) {
      import('ant-design-vue/dist/antd.less');
    }
    
    // build/vite/plugin/styleImport
    import styleImport from 'vite-plugin-style-import';
    export function configStyleImportPlugin(isBuild: boolean) {
      if (!isBuild) return [];
      const styleImportPlugin = styleImport({
        libs: [
          {
            libraryName: 'ant-design-vue',
            esModule: true,
            resolveStyle: (name) => {
              return `ant-design-vue/es/${name}/style/index`;
            },
          },
        ],
      });
      return styleImportPlugin;
    }