### GET Request Example Source: https://doc.vben.pro/guide/essentials/server.html Example of performing a GET request to fetch user information using the configured request client. ```typescript import { requestClient } from '#/api/request'; export async function getUserInfoApi() { return requestClient.get('/user/info'); } ``` -------------------------------- ### Install Live Server Source: https://doc.vben.pro/guide/essentials/build.html Install the 'live-server' globally to preview the 'dist' folder contents locally. ```bash npm i -g live-server ``` -------------------------------- ### Nginx Configuration for Compression Source: https://doc.vben.pro/guide/essentials/build.html Example Nginx configuration to enable gzip and brotli compression for served files. Ensure necessary Nginx modules are installed. ```nginx http { # 开启gzip gzip on; # 开启gzip_static # gzip_static 开启后可能会报错,需要安装相应的模块, 具体安装方式可以自行查询 # 只有这个开启,vue文件打包的.gz文件才会有效果,否则不需要开启gzip进行打包 gzip_static on; gzip_proxied any; gzip_min_length 1k; gzip_buffers 4 16k; #如果nginx中使用了多层代理 必须设置这个才可以开启gzip。 gzip_http_version 1.0; gzip_comp_level 2; gzip_types text/plain application/javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png; gzip_vary off; gzip_disable "MSIE [1-6]\."; # 开启 brotli压缩 # 需要安装对应的nginx模块,具体安装方式可以自行查询 # 可以与gzip共存不会冲突 brotli on; brotli_comp_level 6; brotli_buffers 16 8k; brotli_min_length 20; brotli_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript application/javascript image/svg+xml; } ``` -------------------------------- ### Basic Route Configuration Example Source: https://doc.vben.pro/guide/essentials/route.html An example of a basic route configuration object with meta properties like icon and title. ```typescript const routes = [ { name: 'HomeIndex', path: '/home/index', meta: { icon: 'mdi:home', title: $t('page.home.index'), }, }, ]; ``` -------------------------------- ### POST/PUT Request Examples Source: https://doc.vben.pro/guide/essentials/server.html Examples demonstrating how to perform POST and PUT requests to save user data. Includes a generic request method for conditional POST/PUT operations. ```typescript import { requestClient } from '#/api/request'; export async function saveUserApi(user: UserInfo) { return requestClient.post('/user', user); } export async function saveUserApi(user: UserInfo) { return requestClient.put('/user', user); } export async function saveUserApi(user: UserInfo) { const url = user.id ? `/user/${user.id}` : '/user/'; return requestClient.request(url, { data: user, // 或者 PUT method: user.id ? 'PUT' : 'POST', }); } ``` -------------------------------- ### Preview with Live Server Source: https://doc.vben.pro/guide/essentials/build.html Navigate to the 'dist' directory and run 'live-server' to start a local preview. You can specify a port using the --port flag. ```bash cd apps/web-antd/dist # 本地预览,默认端口8080 live-server # 指定端口 live-server --port=9000 ``` -------------------------------- ### VCropper Usage Example with Various Cropping Options Source: https://doc.vben.pro/components/common-ui/vben-cropper.html Provides a comprehensive Vue.js example demonstrating how to use the VCropper component. It shows how to obtain cropped images as Blob objects or base64 strings, and how to export images with specific dimensions. ```vue ``` -------------------------------- ### API Request Example with Proxy Source: https://doc.vben.pro/guide/essentials/server.html Example of making an API request using axios with '/api' as the prefix. The request will be proxied to the configured backend server. ```typescript import axios from 'axios'; axios.get('/api/user').then((res) => { console.log(res); }); ``` -------------------------------- ### Configure Production API URL Source: https://doc.vben.pro/guide/essentials/server.html Set the API endpoint in the .env.production file for production environments. This example sets it to 'https://mock-napi.vben.pro/api'. ```bash VITE_GLOB_API_URL=https://mock-napi.vben.pro/api ``` -------------------------------- ### Install Lefthook Git Hooks Source: https://doc.vben.pro/guide/project/standard.html Install the configured Git hooks managed by lefthook. This ensures that checks are run automatically before commits and merges. ```bash pnpm exec lefthook install ``` -------------------------------- ### Setup Vben Vxe Table Adapter Source: https://doc.vben.pro/components/common-ui/vben-vxe-table.html Configure default behaviors, custom renderers, and UI component library integration for vxe-table. This setup includes defining custom cell renderers for images and links. ```typescript import { h } from 'vue'; import { setupVbenVxeTable, useVbenVxeGrid } from '@vben/plugins/vxe-table'; import { Button, Image } from 'antdv-next'; import { useVbenForm } from './form'; setupVbenVxeTable({ configVxeTable: (vxeUI) => { vxeUI.setConfig({ grid: { align: 'center', border: false, columnConfig: { resizable: true, }, minHeight: 180, formConfig: { enabled: false, }, proxyConfig: { autoLoad: true, response: { result: 'items', total: 'total', list: 'items', }, showActiveMsg: true, showResponseMsg: false, }, round: true, showOverflow: true, size: 'small', }, }); vxeUI.renderer.add('CellImage', { renderTableDefault(_renderOpts, params) { const { column, row } = params; return h(Image, { src: row[column.field] }); }, }); vxeUI.renderer.add('CellLink', { renderTableDefault(renderOpts) { const { props } = renderOpts; return h( Button, { size: 'small', type: 'link' }, { default: () => props?.text }, ); }, }); }, useVbenForm, }); export { useVbenVxeGrid }; export type * from '@vben/plugins/vxe-table'; ``` -------------------------------- ### Backend Menu Data Structure Example Source: https://doc.vben.pro/guide/in-depth/access.html Example of the menu data structure expected from the backend for route generation. It includes nested children and meta information like 'title' and 'noBasicLayout'. ```typescript const dashboardMenus = [ { meta: { order: -1, title: 'page.dashboard.title', }, name: 'Dashboard', path: '/', redirect: '/analytics', children: [ { name: 'Analytics', path: '/analytics', // 这里为页面的路径,需要去掉 views/ 和 .vue component: '/dashboard/analytics/index', meta: { affixTab: true, title: 'page.dashboard.analytics', }, }, { name: 'Workspace', path: '/workspace', component: '/dashboard/workspace/index', meta: { title: 'page.dashboard.workspace', }, }, ], }, { name: 'Test', path: '/test', component: '/test/index', meta: { title: 'page.test', // 部分特殊页面如果不需要基础布局(页面顶部和侧边栏),可将noBasicLayout设置为true noBasicLayout: true, }, }, ]; ``` -------------------------------- ### Route Refresh Example Source: https://doc.vben.pro/guide/essentials/route.html Demonstrates how to use the `useRefresh` hook to refresh the current route. Ensure the hook is imported before use. ```vue ``` -------------------------------- ### Adding a New Dynamic Configuration Variable Source: https://doc.vben.pro/guide/essentials/settings.html Provides an example of adding a new VITE_GLOB_* prefixed variable to environment files and defining its type for dynamic configuration. ```bash VITE_GLOB_OTHER_API_URL=https://mock-napi.vben.pro/other-api ``` -------------------------------- ### Vben Query Form Example Source: https://doc.vben.pro/components/common-ui/vben-form.html Demonstrates the creation and configuration of a query form using the useVbenForm hook. This includes defining various input types like text, password, number with suffix, select, and date picker. It also shows how to set up submission handlers and layout options. ```vue ``` -------------------------------- ### Configure Lefthook Commands for Pre-Commit Source: https://doc.vben.pro/guide/project/standard.html Example configuration for lefthook's pre-commit hook, demonstrating parallel execution of linting and formatting commands for staged JavaScript/TypeScript files. ```yaml pre-commit: parallel: true commands: lint-js: run: pnpm oxfmt {staged_files} && pnpm oxlint --fix {staged_files} && pnpm eslint --cache --fix {staged_files} glob: '*.{js,jsx,ts,tsx}' ``` -------------------------------- ### Install External Dependency with pnpm Source: https://doc.vben.pro/guide/essentials/external-module.html Install an external module like 'antdv-next' into a specific package using pnpm. Ensure you are in the correct package directory before running the command. ```bash # cd /path/to/your/package pnpm add antdv-next ``` -------------------------------- ### Basic Vben Vxe Table Usage Source: https://doc.vben.pro/components/common-ui/vben-vxe-table.html Create a basic table using `useVbenVxeGrid` and demonstrate dynamic manipulation of table options like borders, stripes, and loading states. This example includes event handling for cell clicks. ```vue ``` -------------------------------- ### Basic Vben Form Usage Source: https://doc.vben.pro/components/common-ui/vben-form.html Demonstrates the fundamental setup of a Vben Form using the `useVbenForm` hook. It includes defining common configurations, a submit handler, layout options, and a comprehensive schema for various input fields. This snippet is suitable for creating a standard form with diverse input types. ```vue ``` -------------------------------- ### Fixed Aspect Ratio Image Cropping Source: https://doc.vben.pro/components/common-ui/vben-cropper.html Shows how to use VCropper with a fixed aspect ratio using the `aspectRatio` prop. This example includes a dropdown to select different aspect ratios and demonstrates cropping with the chosen ratio. Ensure proper cleanup of object URLs. ```vue ``` -------------------------------- ### Navigate with Custom Page Key Source: https://doc.vben.pro/guide/essentials/route.html Example of navigating to a route and setting a custom `pageKey` in the query parameters to control tab identification. This method has high priority. ```vue ``` -------------------------------- ### Remote Data Loading with Vben Vxe Table Source: https://doc.vben.pro/components/common-ui/vben-vxe-table.html Configure the proxyConfig.ajax.query to implement remote data loading. This example shows how to fetch data asynchronously using a mock API and display it in a Vxe Grid. ```vue ``` -------------------------------- ### VbenTiptap with Image Upload Configuration Source: https://doc.vben.pro/components/common-ui/vben-tiptap.html Configures VbenTiptap to handle image uploads using a custom upload function. This example demonstrates setting accepted file types, max size, and simulating upload progress. ```vue ``` -------------------------------- ### Preview Project Locally (Recommended) Source: https://doc.vben.pro/guide/essentials/build.html Use this command to preview the built project locally before deployment. Access the preview at http://localhost:4173. ```bash pnpm preview ``` -------------------------------- ### Vben Form with Value Formatting Source: https://doc.vben.pro/components/common-ui/vben-form.html Demonstrates using valueFormat with RangePicker and DatePicker components to transform values. Includes examples for splitting a date range into start and end times, and directly returning a timestamp. This snippet requires the useVbenForm hook and Ant Design Vue components. ```vue ``` -------------------------------- ### DELETE Request Example Source: https://doc.vben.pro/guide/essentials/server.html Example of performing a DELETE request to remove a user by their ID. ```typescript import { requestClient } from '#/api/request'; export async function deleteUserApi(userId: number) { return requestClient.delete(`/user/${userId}`); } ``` -------------------------------- ### Build Project Source: https://doc.vben.pro/guide/essentials/build.html Execute this command in the project root to build the project for production. The output will be in the 'dist' folder. ```bash pnpm build ``` -------------------------------- ### Run Documentation Locally Source: https://doc.vben.pro/guide/essentials/development.html Execute this command in the project root to run the documentation site locally, allowing for adjustments and previews. ```bash pnpm dev:docs ``` -------------------------------- ### Basic Drawer Usage Source: https://doc.vben.pro/components/common-ui/vben-drawer.html Demonstrates how to create and open a basic drawer component using the `useVbenDrawer` hook. ```vue ``` -------------------------------- ### Environment File Types Source: https://doc.vben.pro/guide/essentials/settings.html Lists the different types of .env files and their loading rules, consistent with Vite's environment variable handling. ```bash .env # 在所有的环境中被载入 .env.local # 在所有的环境中被载入,但会被 git 忽略 .env.[mode] # 只在指定的模式中被载入 .env.[mode].local # 只在指定的模式中被载入,但会被 git 忽略 ``` -------------------------------- ### Complex Zod Validation with Refinement Source: https://doc.vben.pro/components/common-ui/vben-form.html Example of a complex Zod validation involving minimum length and a custom refinement check. ```typescript // 复杂校验 { z.string() .min(1, { message: '请输入' }) .refine((value) => value === '123', { message: '值必须为123', }); } ``` -------------------------------- ### Accessing Dynamic Configuration Source: https://doc.vben.pro/guide/essentials/settings.html Illustrates how to retrieve dynamic configuration values using the useAppConfig hook, passing environment variables and production status. ```typescript const { apiURL } = useAppConfig(import.meta.env, import.meta.env.PROD); ``` -------------------------------- ### Form Dependencies Configuration Source: https://doc.vben.pro/components/common-ui/vben-form.html Example of configuring form item dependencies using the `dependencies` property in the schema for dynamic field behavior. ```typescript dependencies: { // 触发字段。只有这些字段值变动时,联动才会触发 triggerFields: ['name'], // 动态判断当前字段是否需要显示,不显示则直接销毁 if(values,formApi){}, // 动态判断当前字段是否需要显示,不显示用css隐藏 show(values,formApi){}, // 动态判断当前字段是否需要禁用 disabled(values,formApi){}, // 字段变更时,都会触发该函数 trigger(values,formApi){}, // 动态rules rules(values,formApi){}, // 动态必填 required(values,formApi){}, // 动态组件参数 componentProps(values,formApi){}, } ``` -------------------------------- ### Predefined Form Validation Rule: Required Source: https://doc.vben.pro/components/common-ui/vben-form.html Example of using the predefined 'required' rule for form validation, typically used for mandatory fields. ```typescript { rules: 'required'; } ``` -------------------------------- ### Accessing VCropper Methods via Ref Source: https://doc.vben.pro/components/common-ui/vben-cropper.html Demonstrates how to get a reference to the VCropper component using `ref` in Vue.js to call its methods, such as `getCropImage`. ```vue ``` -------------------------------- ### Predefined Form Validation Rule: Select Required Source: https://doc.vben.pro/components/common-ui/vben-form.html Example of using the predefined 'selectRequired' rule, suitable for fields like dropdowns where a selection is mandatory. ```typescript { rules: 'selectRequired'; } ``` -------------------------------- ### Basic Usage of Vben Descriptions Source: https://doc.vben.pro/components/common-ui/vben-descriptions.html Demonstrates the basic usage of Vben Descriptions by passing an array of items to the `items` prop. Each item includes a `label` and `content`. Columns adapt to breakpoints by default. ```vue ``` -------------------------------- ### Nginx Configuration for API Proxy Source: https://doc.vben.pro/guide/essentials/build.html Example Nginx configuration to proxy API requests from '/api' to your backend server, effectively handling cross-origin issues. ```nginx server { listen 8080; server_name localhost; # 接口代理,用于解决跨域问题 location /api { proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; # 后台接口地址 proxy_pass http://110.110.1.1:8080/api; rewrite "^/api/(.*)$" /$1 break; proxy_redirect default; add_header Access-Control-Allow-Origin *; add_header Access-Control-Allow-Headers X-Requested-With; add_header Access-Control-Allow-Methods GET,POST,OPTIONS; } } ``` -------------------------------- ### Implementing useAppConfig for New Variable Source: https://doc.vben.pro/guide/essentials/settings.html Demonstrates how to update the useAppConfig function to include and return the newly added configuration variable. ```typescript export function useAppConfig( env: Record, isProduction: boolean, ): ApplicationConfig { // 生产环境下,直接使用 window._VBEN_ADMIN_PRO_APP_CONF_ 全局变量 const config = isProduction ? window._VBEN_ADMIN_PRO_APP_CONF_ : (env as VbenAdminProAppConfigRaw); const { VITE_GLOB_API_URL, VITE_GLOB_OTHER_API_URL } = config; return { apiURL: VITE_GLOB_API_URL, otherApiURL: VITE_GLOB_OTHER_API_URL, }; } ```