From 1ce400bb7f9777f6fdd9abb4fbbf7a19f7e7b056 Mon Sep 17 00:00:00 2001 From: neverland Date: Sat, 9 Jul 2022 17:09:54 +0800 Subject: [PATCH] refactor(Toast): redesign function-call API (#10804) --- .../docs/markdown/migrate-from-v3.zh-CN.md | 52 +++++++++ .../vant/docs/markdown/quickstart.en-US.md | 10 +- .../vant/docs/markdown/quickstart.zh-CN.md | 12 +- packages/vant/src/action-bar/README.md | 6 +- packages/vant/src/action-bar/README.zh-CN.md | 6 +- packages/vant/src/action-bar/demo/index.vue | 6 +- packages/vant/src/action-sheet/README.md | 8 +- .../vant/src/action-sheet/README.zh-CN.md | 8 +- packages/vant/src/action-sheet/demo/index.vue | 6 +- .../vant/src/address-edit/AddressEdit.tsx | 4 +- packages/vant/src/address-edit/README.md | 6 +- .../vant/src/address-edit/README.zh-CN.md | 6 +- packages/vant/src/address-edit/demo/index.vue | 6 +- packages/vant/src/address-list/README.md | 6 +- .../vant/src/address-list/README.zh-CN.md | 6 +- packages/vant/src/address-list/demo/index.vue | 6 +- packages/vant/src/calendar/Calendar.tsx | 6 +- packages/vant/src/contact-card/README.md | 8 +- .../vant/src/contact-card/README.zh-CN.md | 8 +- packages/vant/src/contact-card/demo/index.vue | 6 +- packages/vant/src/contact-edit/README.md | 6 +- .../vant/src/contact-edit/README.zh-CN.md | 6 +- packages/vant/src/contact-edit/demo/index.vue | 6 +- packages/vant/src/contact-list/README.md | 8 +- .../vant/src/contact-list/README.zh-CN.md | 8 +- packages/vant/src/contact-list/demo/index.vue | 8 +- packages/vant/src/count-down/README.md | 4 +- packages/vant/src/count-down/README.zh-CN.md | 4 +- packages/vant/src/count-down/demo/index.vue | 4 +- packages/vant/src/coupon-list/demo/index.vue | 4 +- packages/vant/src/form/README.md | 6 +- packages/vant/src/form/README.zh-CN.md | 6 +- packages/vant/src/form/demo/ValidateRules.vue | 6 +- packages/vant/src/image-preview/README.md | 4 +- .../vant/src/image-preview/README.zh-CN.md | 6 +- .../vant/src/image-preview/demo/index.vue | 4 +- packages/vant/src/nav-bar/README.md | 4 +- packages/vant/src/nav-bar/README.zh-CN.md | 4 +- packages/vant/src/nav-bar/demo/index.vue | 6 +- packages/vant/src/number-keyboard/README.md | 6 +- .../vant/src/number-keyboard/README.zh-CN.md | 6 +- .../vant/src/number-keyboard/demo/index.vue | 6 +- packages/vant/src/picker/README.md | 10 +- packages/vant/src/picker/README.zh-CN.md | 10 +- packages/vant/src/picker/demo/index.vue | 8 +- packages/vant/src/popover/README.md | 4 +- packages/vant/src/popover/README.zh-CN.md | 4 +- packages/vant/src/popover/demo/index.vue | 4 +- packages/vant/src/pull-refresh/README.md | 4 +- .../vant/src/pull-refresh/README.zh-CN.md | 4 +- packages/vant/src/pull-refresh/demo/index.vue | 8 +- packages/vant/src/rate/README.md | 4 +- packages/vant/src/rate/README.zh-CN.md | 4 +- packages/vant/src/rate/demo/index.vue | 4 +- packages/vant/src/search/README.md | 12 +- packages/vant/src/search/README.zh-CN.md | 12 +- packages/vant/src/search/demo/index.vue | 8 +- packages/vant/src/share-sheet/README.md | 4 +- packages/vant/src/share-sheet/README.zh-CN.md | 4 +- packages/vant/src/share-sheet/demo/index.vue | 4 +- packages/vant/src/sidebar/README.md | 4 +- packages/vant/src/sidebar/README.zh-CN.md | 4 +- packages/vant/src/sidebar/demo/index.vue | 4 +- packages/vant/src/slider/README.md | 12 +- packages/vant/src/slider/README.zh-CN.md | 12 +- packages/vant/src/slider/demo/index.vue | 4 +- packages/vant/src/stepper/README.md | 6 +- packages/vant/src/stepper/README.zh-CN.md | 6 +- packages/vant/src/stepper/demo/index.vue | 6 +- packages/vant/src/submit-bar/README.md | 10 +- packages/vant/src/submit-bar/README.zh-CN.md | 10 +- packages/vant/src/submit-bar/demo/index.vue | 6 +- packages/vant/src/swipe/README.md | 4 +- packages/vant/src/swipe/README.zh-CN.md | 4 +- packages/vant/src/swipe/demo/index.vue | 4 +- packages/vant/src/tab/README.md | 4 +- packages/vant/src/tab/README.zh-CN.md | 4 +- packages/vant/src/tab/demo/index.vue | 4 +- packages/vant/src/tabbar/README.md | 4 +- packages/vant/src/tabbar/README.zh-CN.md | 4 +- packages/vant/src/tabbar/demo/index.vue | 4 +- packages/vant/src/toast/README.md | 98 ++++++++++------- packages/vant/src/toast/README.zh-CN.md | 104 ++++++++++-------- packages/vant/src/toast/demo/index.vue | 58 +++++----- packages/vant/src/toast/function-call.tsx | 46 ++++---- packages/vant/src/toast/index.ts | 16 ++- packages/vant/src/toast/test/function.spec.ts | 100 ++++++++--------- packages/vant/src/toast/types.ts | 9 +- packages/vant/src/uploader/README.md | 18 +-- packages/vant/src/uploader/README.zh-CN.md | 16 +-- packages/vant/src/uploader/demo/index.vue | 8 +- 91 files changed, 549 insertions(+), 460 deletions(-) diff --git a/packages/vant/docs/markdown/migrate-from-v3.zh-CN.md b/packages/vant/docs/markdown/migrate-from-v3.zh-CN.md index 6f924a237..b6d5ab1c6 100644 --- a/packages/vant/docs/markdown/migrate-from-v3.zh-CN.md +++ b/packages/vant/docs/markdown/migrate-from-v3.zh-CN.md @@ -120,6 +120,58 @@ declare module '@vue/runtime-core' { } ``` +### Toast 调用方式调整 + +Vant 4 中,`Toast` 组件的调用方式也进行了调整,与 `Dialog` 组件的改动一致: + +```js +// Vant 3 +Toast(); // 函数调用 + +// Vant 4 +showToast(); // 函数调用 +Toast; // 组件对象 +``` + +`Toast` 上挂载的其他方法也进行了重命名,新旧 API 的映射关系如下: + +```js +Toast(); // -> showToast() +Toast.fail(); // -> showFailToast() +Toast.success(); // -> showSuccessToast() +Toast.loading(); // -> showLoadingToast() +Toast.clear(); // -> closeToast() +Toast.setDefaultOptions(); // -> setToastDefaultOptions() +Toast.resetDefaultOptions(); // -> resetToastDefaultOptions() +``` + +同时,Vant 4 将不再在 `this` 对象上全局注册 `$toast` 方法,这意味着 `this` 对象上将无法访问到 `$toast`。 + +```js +export default { + mounted() { + // 无效代码 + this.$toast('内容'); + }, +}; +``` + +如果需要全局方法,可以手动在 `app` 对象上注册: + +```js +import { showNotify } from 'vant'; + +// 注册 $notify 方法 +app.config.globalProperties.$toast = showNotify; + +// 添加 TS 类型定义 +declare module '@vue/runtime-core' { + interface ComponentCustomProperties { + $toast: typeof showToast; + } +} +``` + ### Notify 调用方式调整 Vant 4 中,`Notify` 组件的调用方式也进行了调整,与 `Dialog` 组件的改动一致: diff --git a/packages/vant/docs/markdown/quickstart.en-US.md b/packages/vant/docs/markdown/quickstart.en-US.md index 9f38280ff..fca3781c3 100644 --- a/packages/vant/docs/markdown/quickstart.en-US.md +++ b/packages/vant/docs/markdown/quickstart.en-US.md @@ -50,7 +50,7 @@ The easiest way to use Vant is to include a CDN link in the html file, after whi app.use(vant.Lazyload); // Call function component - vant.Toast('Message'); + vant.showToast('Message'); app.mount('#app'); @@ -178,19 +178,19 @@ Some components of Vant are provided as function, including `Toast`, `Dialog`, ` ```js // Toast -import { Toast } from 'vant'; +import { showToast } from 'vant'; import 'vant/es/toast/style'; // Dialog -import { Dialog } from 'vant'; +import { showDialog } from 'vant'; import 'vant/es/dialog/style'; // Notify -import { Notify } from 'vant'; +import { showNotify } from 'vant'; import 'vant/es/notify/style'; // ImagePreview -import { ImagePreview } from 'vant'; +import { showImagePreview } from 'vant'; import 'vant/es/image-preview/style'; ``` diff --git a/packages/vant/docs/markdown/quickstart.zh-CN.md b/packages/vant/docs/markdown/quickstart.zh-CN.md index 00826811d..0888589b4 100644 --- a/packages/vant/docs/markdown/quickstart.zh-CN.md +++ b/packages/vant/docs/markdown/quickstart.zh-CN.md @@ -54,8 +54,8 @@ pnpm add vant // 可以通过下面的方式手动注册 app.use(vant.Lazyload); - // 调用函数组件,弹出一个 Toast - vant.Toast('提示'); + // 调用工具函数,弹出一个 Toast + vant.showToast('提示'); app.mount('#app'); @@ -179,19 +179,19 @@ Vant 中有个别组件是以函数的形式提供的,包括 `Toast`,`Dialog ```js // Toast -import { Toast } from 'vant'; +import { showToast } from 'vant'; import 'vant/es/toast/style'; // Dialog -import { Dialog } from 'vant'; +import { showDialog } from 'vant'; import 'vant/es/dialog/style'; // Notify -import { Notify } from 'vant'; +import { showNotify } from 'vant'; import 'vant/es/notify/style'; // ImagePreview -import { ImagePreview } from 'vant'; +import { showImagePreview } from 'vant'; import 'vant/es/image-preview/style'; ``` diff --git a/packages/vant/src/action-bar/README.md b/packages/vant/src/action-bar/README.md index 742c858c5..653851f55 100644 --- a/packages/vant/src/action-bar/README.md +++ b/packages/vant/src/action-bar/README.md @@ -32,12 +32,12 @@ app.use(ActionBarButton); ``` ```js -import { Toast } from 'vant'; +import { showToast } from 'vant'; export default { setup() { - const onClickIcon = () => Toast('Click Icon'); - const onClickButton = () => Toast('Click Button'); + const onClickIcon = () => showToast('Click Icon'); + const onClickButton = () => showToast('Click Button'); return { onClickIcon, onClickButton, diff --git a/packages/vant/src/action-bar/README.zh-CN.md b/packages/vant/src/action-bar/README.zh-CN.md index c529e234b..fac207407 100644 --- a/packages/vant/src/action-bar/README.zh-CN.md +++ b/packages/vant/src/action-bar/README.zh-CN.md @@ -32,12 +32,12 @@ app.use(ActionBarButton); ``` ```js -import { Toast } from 'vant'; +import { showToast } from 'vant'; export default { setup() { - const onClickIcon = () => Toast('点击图标'); - const onClickButton = () => Toast('点击按钮'); + const onClickIcon = () => showToast('点击图标'); + const onClickButton = () => showToast('点击按钮'); return { onClickIcon, onClickButton, diff --git a/packages/vant/src/action-bar/demo/index.vue b/packages/vant/src/action-bar/demo/index.vue index eed313bc9..16c727c47 100644 --- a/packages/vant/src/action-bar/demo/index.vue +++ b/packages/vant/src/action-bar/demo/index.vue @@ -3,7 +3,7 @@ import VanActionBar from '..'; import VanActionBarIcon from '../../action-bar-icon'; import VanActionBarButton from '../../action-bar-button'; import { useTranslate } from '../../../docs/site'; -import { Toast } from '../../toast'; +import { showToast } from '../../toast'; const t = useTranslate({ 'zh-CN': { @@ -33,8 +33,8 @@ const t = useTranslate({ customButtonColor: 'Custom Button Color', }, }); -const onClickIcon = () => Toast(t('clickIcon')); -const onClickButton = () => Toast(t('clickButton')); +const onClickIcon = () => showToast(t('clickIcon')); +const onClickButton = () => showToast(t('clickButton'));