--- url: 'https://cn.vuejs.org/ + https://element-plus.org/zh-CN/' description: Vue 3 + Element Plus 完整中文文档 - Vue 3 渐进式 JavaScript 框架与 Element Plus 企业级组件库完整指南 --- # Vue 3 + Element Plus 完整文档 本文档整合了 Vue 3 核心框架和 Element Plus 组件库的完整使用指南,适用于构建现代化企业级 Web 应用程序。 --- # 第一部分:Vue 3 核心 ## 简介 Vue.js 是一个渐进式 JavaScript 框架,用于构建用户界面。它被设计为可增量采用,可以轻松集成到现有项目中。 ### 核心特性 - **响应式数据绑定**: 自动追踪依赖并更新 DOM - **组件化开发**: 可复用、可组合的 UI 组件 - **虚拟 DOM**: 高效的渲染和更新机制 - **组合式 API**: 更灵活的代码组织方式 - **TypeScript 支持**: 完整的类型推导 --- ## 快速开始 ### 环境要求 - Node.js 版本 ^18.3.0 或 >=20.0.0 - 推荐使用 Visual Studio Code + Vue Official 扩展 ### 创建项目 ```bash npm create vue@latest ``` 该命令将启动官方脚手架工具,提供 TypeScript、Vue Router、Pinia 等可选功能。 ### 安装依赖并启动 ```bash cd your-project npm install npm run dev ``` ### 通过 CDN 使用 ```html ``` --- ## 响应式基础 ### Composition API - ref() ```vue ``` ### Options API - data() ```js export default { data() { return { count: 1 } }, mounted() { console.log(this.count) // => 1 this.count = 2 } } ``` ### reactive() 响应式对象 ```js import { reactive } from 'vue' const state = reactive({ count: 0 }) function increment() { state.count++ } ``` **注意**: 不要替换 reactive 对象的引用,这会导致响应性丢失: ```js // 错误示例 let state = reactive({ count: 0 }) state = reactive({ count: 1 }) // 丢失响应性 ``` ### ref vs reactive 对比 | 特性 | ref | reactive | |------|-----|----------| | 基本类型 | 支持 | 不支持 | | 对象类型 | 支持 | 支持 | | 解构保持响应性 | 需要 toRefs | 不支持 | | 访问方式 | .value | 直接访问 | | 模板自动解包 | 支持 | 支持 | --- ## 计算属性 computed ```vue ``` --- ## 侦听器 watch 和 watchEffect ### watch - 侦听特定数据源 ```js import { ref, watch } from 'vue' const question = ref('') const answer = ref('Questions usually contain a question mark.') // 侦听单个 ref watch(question, async (newQuestion, oldQuestion) => { if (newQuestion.includes('?')) { answer.value = 'Thinking...' try { const res = await fetch('https://yesno.wtf/api') answer.value = (await res.json()).answer } catch (error) { answer.value = 'Error! Could not reach the API.' } } }) // 侦听多个数据源 const x = ref(0) const y = ref(0) watch([x, () => y.value], ([newX, newY]) => { console.log(`x is ${newX} and y is ${newY}`) }) ``` ### watchEffect - 自动追踪依赖 ```js import { ref, watchEffect } from 'vue' const count = ref(0) watchEffect(() => { console.log(`count is: ${count.value}`) }) // 立即执行一次,之后每当 count 变化时执行 ``` ### 停止侦听器 ```js const stop = watchEffect(() => {}) // 当不再需要此侦听器时: stop() ``` ### 侦听器选项 ```js watch(source, callback, { immediate: true, // 立即执行 deep: true, // 深度侦听 flush: 'post', // 在 DOM 更新后执行 once: true // 只触发一次 (3.4+) }) ``` --- ## 模板语法 ### 文本插值 ```vue-html Message: {{ msg }} ``` ### 原始 HTML ```vue-html ``` ### 属性绑定 v-bind ```vue-html
...
``` ### 事件绑定 v-on ```vue-html ... ... ...
...
``` ### 条件渲染 ```vue-html

A

B

Not A/B

Hello!

``` **v-if vs v-show**: - `v-if` 是真正的条件渲染,切换时会销毁和重建元素 - `v-show` 元素始终存在,只是切换 CSS display 属性 - 频繁切换用 `v-show`,条件很少改变用 `v-if` ### 列表渲染 ```vue-html
  • {{ index }} - {{ item.text }}
  • {{ index }}. {{ key }}: {{ value }}
  • {{ n }} ``` ### 双向绑定 v-model ```vue-html ``` --- ## 组件基础 ### 定义组件 ```vue ``` ### Props ```vue ``` ### Emits 事件 ```vue ``` ### 插槽 Slots ```vue

    Main content goes here

    ``` ### 作用域插槽 ```vue ``` --- ## 生命周期钩子 ```vue ``` ### 生命周期顺序 1. setup() 2. onBeforeMount 3. onMounted 4. onBeforeUpdate (数据变化时) 5. onUpdated 6. onBeforeUnmount 7. onUnmounted --- ## 依赖注入 Provide / Inject ```vue ``` --- ## 组合式函数 (Composables) ```js // composables/useMouse.js import { ref, onMounted, onUnmounted } from 'vue' export function useMouse() { const x = ref(0) const y = ref(0) function update(event) { x.value = event.pageX y.value = event.pageY } onMounted(() => window.addEventListener('mousemove', update)) onUnmounted(() => window.removeEventListener('mousemove', update)) return { x, y } } // 在组件中使用 ``` ### 异步组合式函数 ```js // composables/useFetch.js import { ref, watchEffect, toValue } from 'vue' export function useFetch(url) { const data = ref(null) const error = ref(null) const loading = ref(false) watchEffect(async () => { loading.value = true data.value = null error.value = null try { const res = await fetch(toValue(url)) data.value = await res.json() } catch (e) { error.value = e } finally { loading.value = false } }) return { data, error, loading } } ``` --- ## 自定义指令 ```vue ``` ### 全局注册指令 ```js app.directive('focus', { mounted(el) { el.focus() } }) ``` --- ## 异步组件 ```js import { defineAsyncComponent } from 'vue' // 基础用法 const AsyncComp = defineAsyncComponent(() => import('./components/MyComponent.vue') ) // 高级用法 const AsyncCompWithOptions = defineAsyncComponent({ loader: () => import('./Foo.vue'), loadingComponent: LoadingComponent, errorComponent: ErrorComponent, delay: 200, // 显示加载组件前的延迟 timeout: 3000, // 超时时间 suspensible: false }) ``` --- # 第二部分:Element Plus 组件库 ## 简介 Element Plus 是一套为设计师和开发者准备的基于 Vue 3 的桌面端组件库,提供了丰富的 UI 组件来高效构建应用程序。 ### 设计原则 1. **一致性 (Consistency)**: 与现实生活的流程、逻辑保持一致,遵循用户习惯的语言和概念 2. **反馈 (Feedback)**: 通过界面样式和交互动效让用户可以清晰地感知自己的操作 3. **效率 (Efficiency)**: 简化流程,让用户的操作更简单直接 4. **可控 (Controllability)**: 用户可自由操作,包括撤销、回退等 --- ## 安装和配置 ### 包管理器安装 ```bash # npm npm install element-plus # yarn yarn add element-plus # pnpm pnpm install element-plus ``` ### 完整引入 ```typescript // main.ts import { createApp } from 'vue' import ElementPlus from 'element-plus' import 'element-plus/dist/index.css' import App from './App.vue' const app = createApp(App) app.use(ElementPlus) app.mount('#app') ``` ### 按需引入 (推荐) 安装插件: ```bash npm install -D unplugin-vue-components unplugin-auto-import ``` **Vite 配置**: ```typescript // vite.config.ts import { defineConfig } from 'vite' import AutoImport from 'unplugin-auto-import/vite' import Components from 'unplugin-vue-components/vite' import { ElementPlusResolver } from 'unplugin-vue-components/resolvers' export default defineConfig({ plugins: [ AutoImport({ resolvers: [ElementPlusResolver()], }), Components({ resolvers: [ElementPlusResolver()], }), ], }) ``` **Webpack 配置**: ```javascript // webpack.config.js const AutoImport = require('unplugin-auto-import/webpack') const Components = require('unplugin-vue-components/webpack') const { ElementPlusResolver } = require('unplugin-vue-components/resolvers') module.exports = { plugins: [ AutoImport({ resolvers: [ElementPlusResolver()], }), Components({ resolvers: [ElementPlusResolver()], }), ], } ``` ### TypeScript 支持 ```json // tsconfig.json { "compilerOptions": { "types": ["element-plus/global"] } } ``` --- ## 全局配置 ### ConfigProvider 组件 ```vue ``` ### 全局属性 ```typescript app.use(ElementPlus, { size: 'small', zIndex: 3000 }) ``` --- ## 基础组件 ### Button 按钮 ```vue ``` **Button 属性**: | 属性 | 说明 | 类型 | 默认值 | |------|------|------|--------| | type | 类型 | 'primary'/'success'/'warning'/'danger'/'info' | — | | size | 尺寸 | 'large'/'default'/'small' | — | | plain | 是否朴素按钮 | boolean | false | | text | 是否文字按钮 | boolean | false | | link | 是否链接按钮 | boolean | false | | round | 是否圆角按钮 | boolean | false | | circle | 是否圆形按钮 | boolean | false | | loading | 是否加载中 | boolean | false | | disabled | 是否禁用 | boolean | false | | icon | 图标组件 | string/Component | — | | color | 自定义颜色 | string | — | ### Icon 图标 ```bash # 安装图标库 npm install @element-plus/icons-vue ``` ```vue ``` ### Link 链接 ```vue ``` --- ## 布局组件 ### Container 布局容器 ```vue ``` ### Layout 栅格布局 ```vue ``` ### Space 间距 ```vue ``` --- ## 表单组件 ### Form 表单 ```vue ``` **Form 属性**: | 属性 | 说明 | 类型 | 默认值 | |------|------|------|--------| | model | 表单数据对象 | object | — | | rules | 表单验证规则 | object | — | | inline | 行内表单模式 | boolean | false | | label-position | 标签位置 | 'left'/'right'/'top' | right | | label-width | 标签宽度 | string/number | — | | size | 尺寸 | 'large'/'default'/'small' | — | | disabled | 禁用表单 | boolean | false | | scroll-to-error | 验证失败滚动到错误项 | boolean | false | **Form 方法**: | 方法名 | 说明 | |--------|------| | validate | 对整个表单进行验证 | | validateField | 对指定字段进行验证 | | resetFields | 重置表单到初始值 | | clearValidate | 清除验证结果 | | scrollToField | 滚动到指定字段 | ### Input 输入框 ```vue ``` ### Select 选择器 ```vue ``` ### DatePicker 日期选择器 ```vue ``` ### 其他表单组件 ```vue ``` --- ## 数据展示组件 ### Table 表格 ```vue ``` **带边框和斑马纹**: ```vue ``` **固定表头和列**: ```vue ``` **多选表格**: ```vue ``` **排序和筛选**: ```vue ``` **展开行**: ```vue ``` **树形数据**: ```vue ``` **自定义列模板**: ```vue ``` ### Tree 树形控件 ```vue ``` ### Pagination 分页 ```vue ``` ### 其他数据展示组件 ```vue ``` --- ## 导航组件 ### Menu 菜单 ```vue ``` ### Tabs 标签页 ```vue ``` ### Breadcrumb 面包屑 ```vue ``` ### Dropdown 下拉菜单 ```vue ``` ### Steps 步骤条 ```vue ``` --- ## 反馈组件 ### Dialog 对话框 ```vue ``` ### Drawer 抽屉 ```vue ``` ### Message 消息提示 ```vue ``` ### MessageBox 弹框 ```vue ``` ### Notification 通知 ```vue ``` ### Loading 加载 ```vue ``` ### Popover 弹出框 ```vue ``` ### Tooltip 文字提示 ```vue ``` ### Alert 警告 ```vue ``` ### Skeleton 骨架屏 ```vue ``` --- ## 主题定制 ### CSS 变量方案 ```css /* 全局修改 */ :root { --el-color-primary: green; --el-color-success: #67c23a; --el-color-warning: #e6a23c; --el-color-danger: #f56c6c; --el-color-info: #909399; } /* 组件级别修改 */ .custom-class { --el-tag-bg-color: red; } ``` ### JavaScript 动态修改 ```typescript const el = document.documentElement // 获取 CSS 变量 const primaryColor = getComputedStyle(el).getPropertyValue('--el-color-primary') // 设置 CSS 变量 el.style.setProperty('--el-color-primary', 'red') ``` ### SCSS 变量方案 ```scss // styles/element/index.scss @forward 'element-plus/theme-chalk/src/common/var.scss' with ( $colors: ( 'primary': ( 'base': green, ), ), ); ``` **Vite 配置**: ```typescript // vite.config.ts export default defineConfig({ css: { preprocessorOptions: { scss: { additionalData: `@use "~/styles/element/index.scss" as *;`, }, }, }, }) ``` ### 暗黑模式 ```typescript // main.ts import 'element-plus/theme-chalk/dark/css-vars.css' ``` ```html ``` **动态切换** (使用 VueUse): ```vue ``` **自定义暗黑模式变量**: ```css html.dark { --el-bg-color: #141414; --el-bg-color-page: #0a0a0a; --el-bg-color-overlay: #1d1e1f; } ``` --- ## 国际化 ### 全局配置 ```typescript // main.ts import ElementPlus from 'element-plus' import zhCn from 'element-plus/es/locale/lang/zh-cn' app.use(ElementPlus, { locale: zhCn, }) ``` ### ConfigProvider 方式 ```vue ``` ### Day.js 本地化 ```typescript import 'dayjs/locale/zh-cn' ``` ### 支持的语言 Element Plus 支持 60+ 种语言,包括: - 简体中文 (zh-cn) - 繁体中文 (zh-tw) - 英语 (en) - 日语 (ja) - 韩语 (ko) - 法语 (fr) - 德语 (de) - 西班牙语 (es) - 等等... --- ## 最佳实践 ### 项目结构 ``` src/ ├── assets/ ├── components/ │ └── common/ ├── composables/ ├── layouts/ ├── pages/ ├── router/ ├── stores/ ├── styles/ │ └── element/ │ └── index.scss ├── utils/ ├── App.vue └── main.ts ``` ### 按需引入配置 ```typescript // vite.config.ts import { defineConfig } from 'vite' import vue from '@vitejs/plugin-vue' import AutoImport from 'unplugin-auto-import/vite' import Components from 'unplugin-vue-components/vite' import { ElementPlusResolver } from 'unplugin-vue-components/resolvers' export default defineConfig({ plugins: [ vue(), AutoImport({ imports: ['vue', 'vue-router', 'pinia'], resolvers: [ElementPlusResolver()], dts: 'src/auto-imports.d.ts', }), Components({ resolvers: [ElementPlusResolver()], dts: 'src/components.d.ts', }), ], }) ``` ### 与 Vue Router 集成 ```typescript // router/index.ts import { createRouter, createWebHistory } from 'vue-router' const router = createRouter({ history: createWebHistory(), routes: [ { path: '/', component: () => import('@/layouts/DefaultLayout.vue'), children: [ { path: '', name: 'Home', component: () => import('@/pages/Home.vue'), }, ], }, ], }) export default router ``` ### 与 Pinia 集成 ```typescript // stores/user.ts import { defineStore } from 'pinia' import { ref, computed } from 'vue' export const useUserStore = defineStore('user', () => { const user = ref(null) const isLoggedIn = computed(() => !!user.value) function setUser(userData) { user.value = userData } function logout() { user.value = null } return { user, isLoggedIn, setUser, logout } }) ``` ### 表单封装示例 ```vue ``` ### 表格封装示例 ```vue ``` --- ## 内置过渡动画 Element Plus 提供了一系列内置的过渡动画: ```vue ``` **可用的过渡名称**: - `el-fade-in-linear` - `el-fade-in` - `el-zoom-in-center` - `el-zoom-in-top` - `el-zoom-in-bottom` --- ## TypeScript 支持 ### 类型导入 ```typescript import type { FormInstance, FormRules, TableInstance, TableColumnCtx, UploadProps, UploadFile, ComponentSize, } from 'element-plus' ``` ### 组件 Ref 类型 ```typescript import { ref } from 'vue' import type { FormInstance, TableInstance } from 'element-plus' const formRef = ref() const tableRef = ref() ``` ### Props 类型 ```typescript import type { FormProps, TableProps } from 'element-plus' const formProps: Partial = { labelWidth: '100px', labelPosition: 'right', } ``` --- ## 补充组件 ### Cascader 级联选择器 ```vue ``` **Cascader 属性**: | 属性 | 说明 | 类型 | 默认值 | |------|------|------|--------| | v-model | 选中项绑定值 | array | — | | options | 可选项数据源 | array | — | | props | 配置选项 | object | — | | clearable | 是否可清空 | boolean | false | | show-all-levels | 显示完整路径 | boolean | true | | collapse-tags | 多选时折叠标签 | boolean | false | | filterable | 是否可搜索 | boolean | false | | disabled | 是否禁用 | boolean | false | **Props 配置项**: | 属性 | 说明 | 类型 | 默认值 | |------|------|------|--------| | expandTrigger | 展开方式 | 'click'/'hover' | 'click' | | multiple | 是否多选 | boolean | false | | checkStrictly | 可选任意级别 | boolean | false | | emitPath | 选中值为完整路径 | boolean | true | | lazy | 是否懒加载 | boolean | false | | lazyLoad | 懒加载函数 | function | — | | value | 值字段名 | string | 'value' | | label | 标签字段名 | string | 'label' | | children | 子节点字段名 | string | 'children' | --- ### InputNumber 数字输入框 ```vue ``` **InputNumber 属性**: | 属性 | 说明 | 类型 | 默认值 | |------|------|------|--------| | v-model | 绑定值 | number | — | | min | 最小值 | number | -Infinity | | max | 最大值 | number | Infinity | | step | 步长 | number | 1 | | step-strictly | 严格步数 | boolean | false | | precision | 数值精度 | number | — | | controls | 是否使用控制按钮 | boolean | true | | controls-position | 按钮位置 | 'right'/'' | — | | size | 尺寸 | 'large'/'default'/'small' | default | | disabled | 禁用 | boolean | false | | readonly | 只读 | boolean | false | | placeholder | 占位文本 | string | — | --- ### Autocomplete 自动补全 ```vue ``` **Autocomplete 属性**: | 属性 | 说明 | 类型 | 默认值 | |------|------|------|--------| | v-model | 绑定值 | string | — | | fetch-suggestions | 获取建议数据方法 | function | — | | trigger-on-focus | 聚焦时显示建议 | boolean | true | | clearable | 可清空 | boolean | false | | disabled | 禁用 | boolean | false | | debounce | 防抖延迟(ms) | number | 300 | | placement | 弹出位置 | string | 'bottom-start' | | highlight-first-item | 高亮第一项 | boolean | false | --- ### TreeSelect 树形选择 ```vue ``` --- ### Mention 提及 ```vue ``` **Mention 属性**: | 属性 | 说明 | 类型 | 默认值 | |------|------|------|--------| | v-model | 绑定值 | string | — | | options | 选项数据 | array | [] | | prefix | 触发字符 | string/array | '@' | | split | 分隔符 | string | ' ' | | type | 输入框类型 | 'text'/'textarea' | 'text' | | loading | 加载状态 | boolean | false | | whole | 整体删除 | boolean | false | **事件**: - `@search`: 触发搜索时 - `@select`: 选中时 --- ### InputTag 标签输入 ```vue ``` **InputTag 属性**: | 属性 | 说明 | 类型 | 默认值 | |------|------|------|--------| | v-model | 绑定值 | array | — | | max | 最大标签数 | number | — | | trigger | 触发键 | 'Enter'/'Space' | 'Enter' | | tag-type | 标签类型 | string | 'info' | | draggable | 可拖拽 | boolean | false | | delimiter | 分隔符 | string/regexp | — | | collapse-tags | 折叠标签 | boolean | false | | clearable | 可清空 | boolean | false | | disabled | 禁用 | boolean | false | --- ### SelectV2 虚拟化选择器 用于大数据量场景,支持虚拟滚动优化性能。 ```vue ``` **SelectV2 属性**: | 属性 | 说明 | 类型 | 默认值 | |------|------|------|--------| | v-model | 绑定值 | string/array | — | | options | 选项数据 | array | — | | multiple | 是否多选 | boolean | false | | filterable | 是否可筛选 | boolean | false | | clearable | 是否可清空 | boolean | false | | disabled | 禁用 | boolean | false | | remote | 远程搜索 | boolean | false | | allow-create | 允许创建 | boolean | false | --- ### TableV2 虚拟化表格 用于渲染大数据量表格,使用虚拟滚动优化性能。 ```vue ``` **注意**: TableV2 仍处于 Beta 阶段,生产环境使用需谨慎。 --- ### TreeV2 虚拟化树 用于渲染大数据量树形结构。 ```vue ``` **TreeV2 属性**: | 属性 | 说明 | 类型 | 默认值 | |------|------|------|--------| | data | 数据 | array | — | | props | 配置选项 | object | — | | height | 容器高度 | number | 200 | | item-size | 节点高度 | number | 26 | | show-checkbox | 显示复选框 | boolean | false | | check-strictly | 父子不关联 | boolean | false | | filter-method | 筛选方法 | function | — | --- ### Calendar 日历 ```vue ``` **Calendar 属性**: | 属性 | 说明 | 类型 | 默认值 | |------|------|------|--------| | v-model | 绑定值 | Date | — | | range | 日期范围 | array | — | --- ### Statistic 统计数值 ```vue ``` **Statistic 属性**: | 属性 | 说明 | 类型 | 默认值 | |------|------|------|--------| | value | 数值 | number | 0 | | title | 标题 | string | — | | prefix | 前缀 | string | — | | suffix | 后缀 | string | — | | precision | 精度 | number | 0 | | group-separator | 千分位分隔符 | string | ',' | | decimal-separator | 小数点 | string | '.' | | value-style | 数值样式 | object | — | **Countdown 属性**: | 属性 | 说明 | 类型 | 默认值 | |------|------|------|--------| | value | 目标时间 | number/Dayjs | — | | format | 格式化 | string | 'HH:mm:ss' | --- ### Segmented 分段控制器 ```vue ``` --- ### Affix 固钉 ```vue ``` **Affix 属性**: | 属性 | 说明 | 类型 | 默认值 | |------|------|------|--------| | offset | 偏移距离 | number | 0 | | position | 固定位置 | 'top'/'bottom' | 'top' | | target | 容器 | string | — | | z-index | 层级 | number | 100 | --- ### Anchor 锚点 ```vue ``` **Anchor 属性**: | 属性 | 说明 | 类型 | 默认值 | |------|------|------|--------| | container | 滚动容器 | string/HTMLElement | — | | offset | 偏移量 | number | 0 | | duration | 动画时长(ms) | number | 300 | | marker | 显示标记 | boolean | true | | direction | 方向 | 'vertical'/'horizontal' | 'vertical' | --- ### Backtop 回到顶部 ```vue ``` **Backtop 属性**: | 属性 | 说明 | 类型 | 默认值 | |------|------|------|--------| | target | 触发滚动的对象 | string | — | | visibility-height | 显示时机 | number | 200 | | right | 右边距 | number | 40 | | bottom | 下边距 | number | 40 | --- ### PageHeader 页头 ```vue ``` **PageHeader 属性**: | 属性 | 说明 | 类型 | 默认值 | |------|------|------|--------| | icon | 图标 | string/Component | Back | | title | 标题 | string | '' | | content | 内容 | string | '' | **插槽**: icon, title, content, extra, breadcrumb, default --- ### Tour 漫游式引导 ```vue ``` **Tour 属性**: | 属性 | 说明 | 类型 | 默认值 | |------|------|------|--------| | v-model | 显示状态 | boolean | false | | mask | 遮罩 | boolean/object | true | | type | 类型 | 'default'/'primary' | 'default' | | placement | 位置 | string | 'bottom' | **TourStep 属性**: | 属性 | 说明 | 类型 | 默认值 | |------|------|------|--------| | target | 目标元素 | HTMLElement/string/function | — | | title | 标题 | string | — | | description | 描述 | string | — | --- ### Popconfirm 气泡确认框 ```vue ``` **Popconfirm 属性**: | 属性 | 说明 | 类型 | 默认值 | |------|------|------|--------| | title | 标题 | string | — | | confirm-button-text | 确认按钮文字 | string | — | | cancel-button-text | 取消按钮文字 | string | — | | confirm-button-type | 确认按钮类型 | string | 'primary' | | cancel-button-type | 取消按钮类型 | string | 'text' | | icon | 图标 | string/Component | QuestionFilled | | icon-color | 图标颜色 | string | '#f90' | | hide-icon | 隐藏图标 | boolean | false | --- ### Divider 分割线 ```vue ``` **Divider 属性**: | 属性 | 说明 | 类型 | 默认值 | |------|------|------|--------| | direction | 方向 | 'horizontal'/'vertical' | 'horizontal' | | border-style | 边框样式 | string | 'solid' | | content-position | 文字位置 | 'left'/'center'/'right' | 'center' | --- ### Watermark 水印 ```vue ``` **Watermark 属性**: | 属性 | 说明 | 类型 | 默认值 | |------|------|------|--------| | content | 水印文字 | string/array | 'Element Plus' | | image | 图片地址 | string | — | | width | 宽度 | number | 120 | | height | 高度 | number | 64 | | rotate | 旋转角度 | number | -22 | | z-index | 层级 | number | 9 | | gap | 间距 | array | [100, 100] | | font | 字体配置 | object | — | --- ### Scrollbar 滚动条 ```vue ``` **Scrollbar 属性**: | 属性 | 说明 | 类型 | 默认值 | |------|------|------|--------| | height | 高度 | string/number | — | | max-height | 最大高度 | string/number | — | | native | 使用原生滚动条 | boolean | false | | always | 始终显示滚动条 | boolean | false | | min-size | 滚动条最小尺寸 | number | 20 | **方法**: - `setScrollTop(top)`: 设置滚动位置 - `setScrollLeft(left)`: 设置水平位置 - `scrollTo(options)`: 滚动到指定位置 - `update()`: 更新滚动条状态 --- ### InfiniteScroll 无限滚动 ```vue ``` **指令属性**: | 属性 | 说明 | 类型 | 默认值 | |------|------|------|--------| | v-infinite-scroll | 加载函数 | function | — | | infinite-scroll-disabled | 是否禁用 | boolean | false | | infinite-scroll-delay | 节流延迟(ms) | number | 200 | | infinite-scroll-distance | 触发距离(px) | number | 0 | | infinite-scroll-immediate | 立即执行 | boolean | true | **注意**: 此指令在 3.0.0 版本将被移除,建议使用 Scrollbar 组件的无限滚动功能。 --- ### Splitter 分割面板 ```vue ``` **Splitter 属性**: | 属性 | 说明 | 类型 | 默认值 | |------|------|------|--------| | layout | 布局方向 | 'horizontal'/'vertical' | 'horizontal' | | lazy | 延迟更新 | boolean | false | **SplitterPanel 属性**: | 属性 | 说明 | 类型 | 默认值 | |------|------|------|--------| | size | 尺寸 | number/string | — | | min | 最小尺寸 | number/string | — | | max | 最大尺寸 | number/string | — | | resizable | 可调整 | boolean | true | | collapsible | 可折叠 | boolean | false | --- ### Text 文本 ```vue ``` **Text 属性**: | 属性 | 说明 | 类型 | 默认值 | |------|------|------|--------| | type | 类型 | 'primary'/'success'/'info'/'warning'/'danger' | — | | size | 尺寸 | 'large'/'default'/'small' | 'default' | | truncated | 显示省略号 | boolean | false | | line-clamp | 最大行数 | number/string | — | | tag | 自定义标签 | string | 'span' | --- ## 常见问题 ### 1. 按需引入样式丢失 确保安装并配置了 `unplugin-element-plus` 插件: ```bash npm install -D unplugin-element-plus ``` ```typescript // vite.config.ts import ElementPlus from 'unplugin-element-plus/vite' export default defineConfig({ plugins: [ElementPlus()], }) ``` ### 2. 日期选择器中文问题 导入 Day.js 的中文语言包: ```typescript import 'dayjs/locale/zh-cn' ``` ### 3. 图标不显示 确保正确安装并导入了图标: ```bash npm install @element-plus/icons-vue ``` ```typescript import * as ElementPlusIconsVue from '@element-plus/icons-vue' const app = createApp(App) for (const [key, component] of Object.entries(ElementPlusIconsVue)) { app.component(key, component) } ``` ### 4. 表单验证不触发 确保 `el-form-item` 的 `prop` 属性与 `model` 中的字段名一致: ```vue ``` ### 5. Dialog 嵌套问题 在嵌套的 Dialog 上添加 `append-to-body` 属性: ```vue 内层对话框 ``` --- 本文档涵盖了 Vue 3 + Element Plus 的核心功能和常用组件,可作为开发企业级 Web 应用的完整参考。