配置提供者
Config Provider 用于提供全局配置,使您的整个应用程序可以在任何地方访问这些配置。
国际化配置
通过Config Provider配置i18n相关属性,获得语言切换功能。
使用两个属性提供 i18n 相关配置
暂无数据
- 1
- 2
- 3
- 4
- 5
- 6
- 10
页
共 100 条
vue
<template>
<div>
<el-button mb-2 @click="toggle">Switch Language</el-button>
<br />
<el-config-provider :locale="locale">
<el-table mb-1 :data="[]" />
<el-pagination :total="100" />
</el-config-provider>
</div>
</template>
<script lang="ts" setup>
import { computed, ref } from 'vue'
import zhCn from 'element-plus-mobile/es/locale/lang/zh-cn'
import en from 'element-plus-mobile/es/locale/lang/en'
const language = ref('zh-cn')
const locale = computed(() => (language.value === 'zh-cn' ? zhCn : en))
const toggle = () => {
language.value = language.value === 'zh-cn' ? 'en' : 'zh-cn'
}
</script>
隐藏来源
按钮配置
vue
<template>
<div>
<div>
<el-checkbox v-model="config.autoInsertSpace">
autoInsertSpace
</el-checkbox>
<el-checkbox v-model="config.plain"> plain </el-checkbox>
<el-checkbox v-model="config.round"> round </el-checkbox>
<el-checkbox v-model="config.dashed"> dashed </el-checkbox>
<el-checkbox v-model="config.text"> text </el-checkbox>
<el-select v-model="config.type" class="ml-5" style="max-width: 150px">
<el-option
v-for="type in buttonTypes.filter(Boolean)"
:key="type"
:value="type"
/>
</el-select>
</div>
<el-divider />
<el-config-provider :button="config">
<el-button>中文</el-button>
</el-config-provider>
</div>
</template>
<script lang="ts" setup>
import { reactive } from 'vue'
import { buttonTypes } from 'element-plus-mobile'
import type { ButtonConfigContext } from 'element-plus-mobile'
const config = reactive<ButtonConfigContext>({
autoInsertSpace: true,
type: 'default',
plain: true,
round: true,
text: false,
dashed: false,
})
</script>
隐藏来源
链接配置 2.9.11
Type:
Underline:
vue
<template>
<div>
<div class="flex gap-4">
<div class="flex flex-col basis-150px gap-1">
<span>Type:</span>
<el-select v-model="config.type">
<el-option v-for="type in linkTypes" :key="type" :value="type" />
</el-select>
</div>
<div class="flex flex-col basis-150px gap-1">
<span>Underline:</span>
<el-select v-model="config.underline">
<el-option
v-for="type in underlineOptions"
:key="type"
:value="type"
/>
</el-select>
</div>
</div>
<el-divider />
<el-config-provider :link="config">
<el-link>Link desu!</el-link>
</el-config-provider>
</div>
</template>
<script lang="ts" setup>
import { reactive } from 'vue'
import type { LinkConfigContext } from 'element-plus-mobile'
const linkTypes = ['primary', 'success', 'warning', 'info', 'danger', 'default']
const underlineOptions = ['always', 'never', 'hover']
const config = reactive<LinkConfigContext>({
type: 'success',
underline: 'always',
})
</script>
隐藏来源
卡配置 2.10.5
Shadow:
Card desu!
vue
<script lang="ts" setup>
import { reactive } from 'vue'
import type { CardConfigContext } from 'element-plus-mobile'
const config = reactive<CardConfigContext>({
shadow: 'always',
})
</script>
<template>
Shadow:
<div class="flex flex-col justify-center">
<el-radio-group v-model="config.shadow">
<el-radio value="always">always</el-radio>
<el-radio value="hover">hover</el-radio>
<el-radio value="never">never</el-radio>
</el-radio-group>
<el-divider />
<el-config-provider :card="config">
<el-card>Card desu!</el-card>
</el-config-provider>
</div>
</template>
隐藏来源
对话框配置 2.10.7
alignCenter
draggable
overflow
enable transition
transition: stringtransition: object
vue
<script lang="ts" setup>
import { computed, nextTick, ref, shallowReactive } from 'vue'
import type { ButtonInstance, DialogTransition } from 'element-plus-mobile'
type GlobalConfig = {
alignCenter: boolean
draggable: boolean
overflow: boolean
transition?: DialogTransition
}
const config = shallowReactive<GlobalConfig>({
alignCenter: false,
draggable: false,
overflow: false,
})
const visible = ref(false)
const enableTransition = ref(false)
const isObjectTransition = ref(false)
const buttonRef = ref<ButtonInstance>()
const ANIMATION_DURATION = 300
const globalConfig = computed<GlobalConfig>(() => {
let transition: DialogTransition | undefined
if (enableTransition.value) {
if (isObjectTransition.value) {
transition = {
css: false,
onBeforeEnter(el) {
nextTick(() => {
if (buttonRef.value) {
const buttonRect = buttonRef.value.ref!.getBoundingClientRect()
const dialogEl = el.querySelector('.el-dialog') as HTMLElement
if (dialogEl) {
const dialogRect = dialogEl.getBoundingClientRect()
const offsetX =
buttonRect.left +
buttonRect.width / 2 -
(dialogRect.left + dialogRect.width / 2)
const offsetY =
buttonRect.top +
buttonRect.height / 2 -
(dialogRect.top + dialogRect.height / 2)
dialogEl.style.transform = `translate(${offsetX}px, ${offsetY}px) scale(0.3)`
dialogEl.style.opacity = '0'
}
}
})
},
onEnter(el, done) {
nextTick(() => {
const dialogEl = el.querySelector('.el-dialog') as HTMLElement
if (dialogEl) {
// force reflow
dialogEl.offsetHeight
dialogEl.style.transition = `all ${ANIMATION_DURATION}ms cubic-bezier(0.4, 0, 1, 1)`
dialogEl.style.transform = 'translate(0, 0) scale(1)'
dialogEl.style.opacity = '1'
// wait for animation to complete, then cleanup inline styles to avoid affecting drag
setTimeout(() => {
dialogEl.style.transition = ''
dialogEl.style.transform = ''
dialogEl.style.opacity = ''
done()
}, ANIMATION_DURATION)
} else {
done()
}
})
},
onLeave(el, done) {
const dialogEl = el.querySelector('.el-dialog') as HTMLElement
if (dialogEl) {
if (buttonRef.value) {
const buttonRect = buttonRef.value.ref!.getBoundingClientRect()
const dialogRect = dialogEl.getBoundingClientRect()
const currentTransform = dialogEl.style.transform
let dragOffsetX = 0
let dragOffsetY = 0
// avoid draggable effect
if (currentTransform) {
const translateMatch = currentTransform.match(
/translate\(([^,]+),\s*([^)]+)\)/
)
if (translateMatch) {
dragOffsetX = Number.parseFloat(translateMatch[1])
dragOffsetY = Number.parseFloat(translateMatch[2])
}
}
const offsetX =
buttonRect.left +
buttonRect.width / 2 -
(dialogRect.left + dialogRect.width / 2) +
dragOffsetX
const offsetY =
buttonRect.top +
buttonRect.height / 2 -
(dialogRect.top + dialogRect.height / 2) +
dragOffsetY
dialogEl.style.transition = `all ${ANIMATION_DURATION}ms cubic-bezier(0.4, 0, 1, 1)`
dialogEl.style.transform = `translate(${offsetX}px, ${offsetY}px) scale(0.3)`
dialogEl.style.opacity = '0'
// wait for animation to complete, then cleanup inline styles
setTimeout(() => {
dialogEl.style.transition = ''
dialogEl.style.transform = ''
dialogEl.style.opacity = ''
done()
}, ANIMATION_DURATION)
} else {
done()
}
} else {
done()
}
},
}
} else {
transition = 'dialog-bounce'
}
}
return {
alignCenter: config.alignCenter,
draggable: config.draggable,
overflow: config.overflow,
transition,
}
})
</script>
<template>
<div class="flex flex-col gap-4 justify-center">
<div class="flex items-center gap-2">
<el-switch v-model="config.alignCenter" active-text="alignCenter" />
</div>
<div class="flex items-center gap-4">
<el-switch v-model="config.draggable" active-text="draggable" />
<el-switch
v-model="config.overflow"
:disabled="!config.draggable"
active-text="overflow"
/>
</div>
<div class="flex items-center gap-2">
<el-switch v-model="enableTransition" active-text="enable transition" />
<el-switch
v-model="isObjectTransition"
:disabled="!enableTransition"
active-text="transition: object"
inactive-text="transition: string"
/>
</div>
<div class="flex items-center gap-2">
<el-button
ref="buttonRef"
type="primary"
size="small"
@click="visible = true"
>
Open Dialog
</el-button>
</div>
<el-config-provider :dialog="globalConfig">
<el-dialog v-model="visible" title="Dialog Title" destroy-on-close>
Dialog Content
</el-dialog>
</el-config-provider>
<div v-if="enableTransition" class="text-xs opacity-70">
<div v-if="isObjectTransition">
Using JavaScript controlled animation:
<code>{{ JSON.stringify(globalConfig.transition) }}</code>
</div>
<div v-else>
Using string transition name:
<code>{{ globalConfig.transition }}</code>
</div>
</div>
</div>
</template>
<style>
/* Bounce Animation */
.dialog-bounce-enter-active,
.dialog-bounce-leave-active,
.dialog-bounce-enter-active .el-dialog,
.dialog-bounce-leave-active .el-dialog {
transition: all 0.5s cubic-bezier(0.175, 0.885, 0.32, 1.275);
}
.dialog-bounce-enter-from,
.dialog-bounce-leave-to {
opacity: 0;
}
.dialog-bounce-enter-from .el-dialog,
.dialog-bounce-leave-to .el-dialog {
transform: scale(0.3) translateY(-50px);
opacity: 0;
}
</style>
隐藏来源
消息配置
vue
<template>
<div>
<el-config-provider :message="config">
<el-button @click="open">OPEN</el-button>
</el-config-provider>
</div>
</template>
<script lang="ts" setup>
import { reactive } from 'vue'
import { ElMessage } from 'element-plus-mobile'
const config = reactive({
max: 3,
plain: true,
placement: 'bottom',
})
const open = () => {
ElMessage('This is a message from bottom.')
}
</script>
隐藏来源
空值配置 2.7.0
支持的组件列表
- 级联
- 颜色选择器2.10.3
- 日期选择器
- 选择
- 选择V2
- 时间选择器
- 时间选择
- 树选择
设置 empty-values 以支持组件的空值。回退值是 ['', null, undefined]。如果您认为空字符串有意义,请写[undefined, null]。
设置value-on-clear设置清零时的返回值。回退值是 undefined。日期部分为 null。如果要设置undefined,请使用() => undefined。
vue
<template>
<el-config-provider :value-on-clear="null" :empty-values="[undefined, null]">
<div class="flex flex-wrap gap-4 items-center">
<el-select
v-model="value1"
clearable
placeholder="Select"
style="width: 240px"
@change="handleChange"
>
<el-option
v-for="item in options"
:key="item.value"
:label="item.label"
:value="item.value"
/>
</el-select>
<el-select-v2
v-model="value2"
clearable
placeholder="Select"
style="width: 240px"
:options="options"
:value-on-clear="() => undefined"
@change="handleChange"
/>
</div>
</el-config-provider>
</template>
<script lang="ts" setup>
import { ref } from 'vue'
import { ElMessage } from 'element-plus-mobile'
const value1 = ref('')
const value2 = ref('')
const options = [
{
value: '',
label: 'All',
},
{
value: 'Option1',
label: 'Option1',
},
{
value: 'Option2',
label: 'Option2',
},
{
value: 'Option3',
label: 'Option3',
},
{
value: 'Option4',
label: 'Option4',
},
{
value: 'Option5',
label: 'Option5',
},
]
const handleChange = (value) => {
if ([undefined, null].includes(value)) {
ElMessage.info(`The clear value is: ${value}`)
}
}
</script>
隐藏来源
表配置 2.13.3
vue
<template>
<div>
<div>
<el-checkbox v-model="config.showOverflowTooltip">
showOverflowTooltip
</el-checkbox>
<el-select
v-model="config.tooltipEffect"
class="ml-5"
style="max-width: 150px"
>
<el-option value="dark" label="dark" />
<el-option value="light" label="light" />
</el-select>
</div>
<el-divider />
<el-config-provider :table="config">
<el-table :data="tableData" style="width: 100%">
<el-table-column type="selection" width="55" />
<el-table-column label="Date" width="120">
<template #default="scope">{{ scope.row.date }}</template>
</el-table-column>
<el-table-column property="name" label="Name" width="120" />
<el-table-column
property="address"
label="Address (inherited from config-provider)"
width="300"
/>
<el-table-column
property="address"
label="Address (explicit false)"
:show-overflow-tooltip="false"
/>
</el-table>
</el-config-provider>
</div>
</template>
<script lang="ts" setup>
import { reactive } from 'vue'
import type { TableConfigContext } from 'element-plus-mobile'
const config = reactive<TableConfigContext>({
showOverflowTooltip: true,
tooltipEffect: 'dark',
})
interface User {
date: string
name: string
address: string
}
const tableData: User[] = [
{
date: '2016-05-04',
name: 'Aleyna Kutzner',
address: 'Lohrbergstr. 86c, Süd Lilli, Saarland',
},
{
date: '2016-05-03',
name: 'Helen Jacobi',
address: '760 A Street, South Frankfield, Illinois',
},
{
date: '2016-05-02',
name: 'Brandon Deckert',
address: 'Arnold-Ohletz-Str. 41a, Alt Malinascheid, Thüringen',
},
{
date: '2016-05-01',
name: 'Margie Smith',
address: '23618 Windsor Drive, West Ricardoview, Idaho',
},
]
</script>
隐藏来源
实验特性
在本节中,您可以学习如何使用 Config Provider 来提供实验性功能。目前,我们还没有添加任何实验性功能,但在功能路线图中,我们将添加一些实验性功能。您可以使用此配置来管理您想要或不想要的功能。
API
Config Provider 属性
| 属性名 | 说明 | 类型 | 默认值 |
|---|---|---|---|
| locale | 语言环境对象,可参考 语言列表 | object | zh |
| size | 全局组件尺寸 | enum | default |
| zIndex | 全局初始 z-index | number | — |
| namespace | 全局组件类名前缀,配合 $namespace 使用 | string | el |
| button | 按钮相关配置,见下表 | object | 见下表 |
| link | 链接相关配置,见下表 | object | 见下表 |
| dialog 2.10.7 | 对话框相关配置,见下表 | object | 见下表 |
| message | 消息相关配置,见下表 | object | 见下表 |
| experimental-features | 实验特性配置,当前所有功能默认关闭 | object | — |
| empty-values 2.7.0 | 组件的全局空值 | array | — |
| value-on-clear 2.7.0 | 全局清空返回值 | string / number / boolean / Function | — |
| table 2.13.3 | 表格相关配置,见下表 | object | 见下表 |
Button 属性
| 属性名 | 说明 | 类型 | 默认值 |
|---|---|---|---|
| type 2.9.11 | 按钮类型,设置 color 时以后者为准 | enum | — |
| autoInsertSpace | 自动在两个汉字之间插入空格(仅当文本长度为 2 且所有字符均为中文时生效) | boolean | false |
| plain 2.9.11 | 是否为朴素按钮 | boolean | false |
| text 2.11.0 | 是否为文本按钮 | boolean | false |
| round 2.9.11 | 是否为圆角按钮 | boolean | false |
| dashed 2.13.3 | 是否为虚线按钮 | boolean | false |
Link 属性
| 属性名 | 说明 | 类型 | 默认值 |
|---|---|---|---|
| type 2.9.11 | 链接类型 | enum | default |
| underline 2.9.11 | 下划线显示策略 | enum | always |
Card 属性
| 属性名 | 说明 | 类型 | 默认值 |
|---|---|---|---|
| shadow 2.10.5 | 何时显示卡片阴影 | enum | — |
Dialog 属性
| 属性名 | 说明 | 类型 | 默认值 |
|---|---|---|---|
| alignCenter 2.10.7 | 是否水平和垂直居中对齐对话框 | boolean | false |
| draggable 2.10.7 | 是否启用对话框拖动 | boolean | false |
| overflow 2.10.7 | 可拖动对话框是否允许超出视口 | boolean | false |
| transition 2.10.7 | 对话框动画过渡配置,可传字符串或 Vue 过渡属性对象 | string / object | — |
Message 属性
| 属性名 | 说明 | 类型 | 默认值 |
|---|---|---|---|
| max | 最多可同时显示的消息条数 | number | — |
| grouping 2.8.2 | 是否合并相同内容的消息,不支持 VNode 消息类型 | boolean | — |
| duration 2.8.2 | 显示持续时间,单位为毫秒;设为 0 时不会自动关闭 | number | — |
| showClose 2.8.2 | 是否显示关闭按钮 | boolean | — |
| offset 2.8.2 | 距离视口顶部的偏移量 | number | — |
| plain 2.9.11 | 是否使用简洁样式 | boolean | — |
| placement 2.11.0 | 消息显示位置 | enum | — |
Table 属性 2.13.3
| 属性名 | 说明 | 类型 | 默认值 |
|---|---|---|---|
| showOverflowTooltip | 是否在内容溢出时通过工具提示展示完整内容,会影响所有表格列 | boolean / object | — |
| tooltipEffect | 溢出工具提示的 effect | enum | dark |
| tooltipOptions | 溢出工具提示配置,参考 Tooltip | object | object |
| tooltipFormatter | 使用 show-overflow-tooltip 时自定义工具提示内容 | Function | — |
Config Provider 插槽
| 插槽名 | 说明 | 类型 |
|---|---|---|
| default | 自定义默认内容 | config:提供的全局配置对象(从顶层继承) |