ConfigProvider全局化配置

ConfigProvider 全局化配置

为组件提供统一的全局化配置。

使用

ConfigProvider 使用 Vue 的 provide / inject 特性,只需在应用外围包裹一次即可全局生效。

<​template>  <​a-config-provider :getPopupContainer="getPopupContainer">    <​app />  <​/a-config-provider><​/template><​script>  export default {    methods: {      getPopupContainer(el, dialogContext) {        if (dialogContext) {          return dialogContext.getDialogWrap();        } else {          return document.body;        }      },    },  };<​/script>

Content Security Policy

部分组件为了支持波纹效果,使用了动态样式。如果开启了 Content Security Policy (CSP),你可以通过 csp 属性来进行配置:

<​a-config-provider :csp="{ nonce: 'YourNonceCode' }">  <​a-button>My Button<​/a-button><​/a-config-provider>

代码演示

国际化

此处列出 Ant Design Vue 中需要国际化支持的组件,你可以在演示里切换语言。

<​template>  <​div class="change-locale">    <​span style="margin-right: 16px">Change locale of components:<​/span>    <​a-radio-group v-model:value="locale">      <​a-radio-button key="en" :value="enUS.locale">English<​/a-radio-button>      <​a-radio-button key="cn" :value="zhCN.locale">中文<​/a-radio-button>    <​/a-radio-group>  <​/div>  <​a-config-provider :locale="locale === 'en' ? enUS : zhCN">    <​div class="locale-components">      <​div class="example">        <​a-pagination :total="50" show-size-changer />      <​/div>      <​div class="example">        <​a-sel​ect show-search style="width: 200px">          <​a-sel​ect-option value="jack">jack<​/a-sel​ect-option>          <​a-sel​ect-option value="lucy">lucy<​/a-sel​ect-option>        <​/a-sel​ect>        <​a-date-picker />        <​a-time-picker />        <​a-range-picker style="width: 200px" />      <​/div>      <​div class="example">        <​a-button type="primary" @click="visible = true">Show Modal<​/a-button>        <​a-button @click="info">Show info<​/a-button>        <​a-button @click="confirm">Show confirm<​/a-button>        <​a-popconfirm title="Question?">          <​a href="#">Click to confirm<​/a>        <​/a-popconfirm>      <​/div>      <​div class="example">        <​a-transfer :data-source="[]" show-search :target-keys="[]" :render="item => item.title" />      <​/div>      <​div style="width: 319px; border: 1px solid #d9d9d9; border-radius: 4px">        <​a-calendar :fullscreen="false" :value="moment()" />      <​/div>      <​div class="example">        <​a-table :data-source="[]" :columns="columns" />      <​/div>      <​a-modal v-model:visible="visible" title="Locale Modal">        <​p>Locale Modal<​/p>      <​/a-modal>    <​/div>  <​/a-config-provider><​/template><​script>import { Modal } from 'ant-design-vue';import enUS from 'ant-design-vue/es/locale/en_US';import zhCN from 'ant-design-vue/es/locale/zh_CN';import moment from 'moment';import 'moment/dist/locale/zh-cn';import { defineComponent, ref, watch } from 'vue';moment.locale('en');const columns = [  {    title: 'Name',    dataIndex: 'name',    filters: [      {        text: 'filter1',        value: 'filter1',      },    ],  },  {    title: 'Age',    dataIndex: 'age',  },];export default defineComponent({  setup() {    const visible = ref(false);    const locale = ref(enUS.locale);    watch(locale, val => {      moment.locale(val);    });    const info = () => {      Modal.info({        title: 'some info',        content: 'some info',      });    };    const confirm = () => {      Modal.confirm({        title: 'some info',        content: 'some info',      });    };    return {      columns,      visible,      locale,      moment,      enUS,      zhCN,      info,      confirm,    };  },});<​/script><​style scoped>.locale-components {  border-top: 1px solid #d9d9d9;  padding-top: 16px;}.example {  margin: 16px 0;}.example > * {  margin-right: 8px;}.change-locale {  margin-bottom: 16px;}<​/style>

API

参数 说明 类型 默认值 版本
autoInsertSpaceInButton 设置为 false 时,移除按钮中 2 个汉字之间的空格 boolean true
csp 设置 Content Security Policy 配置 { nonce: string } -

| renderEmpty | 自定义组件空状态。参考 空状态 | slot-scope | Function(componentName: string): VNode | - | | |---|---|---|---|---|

getPopupContainer 弹出框(Sel​ect, Tooltip, Menu 等等)渲染父节点,默认渲染到 body 上。 Function(triggerNode, dialogContext) () => document.body
locale 语言包配置,语言包可到 ant-design-vue/es/locale 目录下寻找 object - 1.5.0
pageHeader 统一设置 pageHeader 的 ghost,参考 pageHeader { ghost: boolean } ‘true’ 1.5.0
transformCellText Table 数据渲染前可以再次改变,一般用户空数据的默认配置 Function({ text, column, record, index }) => any - 1.5.4 |

FAQ

为什么我使用了 ConfigProvider locale,时间类组件的国际化还有问题?

请检查是否设置了 moment.locale('zh-cn'),或者是否有两个版本的 moment 共存。 如果你使用 Vite 作为构建工具,因 Vite 尚不完善,你需要在你的项目中额外执行 npm i --save moment 进行安装。

配置 getPopupContainer 导致 Modal 报错?

当如下全局设置 getPopupContainer 为触发节点的 parentNode 时,由于 Modal 的用法不存在 triggerNode,这样会导致 triggerNode is undefined 的报错,需要增加一个判断条件。

 <​ConfigProvider-  getPopupContainer={triggerNode => triggerNode.parentNode}+  getPopupContainer={node => {+    if (node) {+      return node.parentNode;+    }+    return document.body;+  }} >   <​App /> <​/ConfigProvider>