/*! For license information please see 1828.5ec27f89.js.LICENSE.txt */ (self.webpackChunk=self.webpackChunk||[]).push([["1828"],{98671:function(e,s,n){"use strict";n.r(s);var a=n("80681");let t=["innerHTML"];s.default={setup:()=>({html:""}),render:()=>((0,a.wg)(),(0,a.iD)("div",{class:"van-doc-markdown-body",innerHTML:'

Upgrade from v3 to v4

\n

Introduction

\n

This document provides an upgrade guide from Vant 3 to Vant 4.

\n

Installing Vant 4

\n

First you need to install Vant 4 and @vant/compat.

\n

@vant/compat is a compatibility package that helps you to switch from Vant 3 to Vant 4.

\n
# Install via npm\nnpm add vant@^4 @vant/compat@^1\n\n# Install via yarn\nyarn add vant@^4 @vant/compat@^1\n\n# Install via pnpm\npnpm add vant@^4 @vant/compat@^1\n\n# Install via Bun\nbun add vant@^4 @vant/compat@^1\n
\n

You can also change the version directly in the dependencies field of package.json, and you will need to reinstall the dependencies after the change.

\n
{\n   "dependencies": {\n-    "vant": "^3.0.0",\n+    "vant": "^4.0.0",\n+    "@vant/compat": "^1.0.0",\n   }\n}\n
\n

Import method changes

\n

Remove babel-plugin-import

\n

Starting from Vant 4.0, babel-plugin-import will no longer be supported, please remove the babel-plugin-import plugin that the project depends on.

\n

Simply remove the following code in babel.config.js:

\n
module.exports = {\n  plugins: [\n-    ['import', {\n-      libraryName: 'vant',\n-      libraryDirectory: 'es',\n-      style: true\n-    }, 'vant']\n  ]\n};\n
\n

Benefits

\n

The main benefits of removing babel-plugin-import are as follows:

\n\n
import { showToast, buttonProps } from 'vant';\n
\n

Import style on demand

\n

Removing babel-plugin-import will not affect the JS size of the project, because Vant supports Tree Shaking to remove unnecessary JS code by default.

\n

The way the CSS code is imported can be selected from the following two ways:

\n\n
import 'vant/lib/index.css';\n
\n\n

Component refactoring

\n

Introduction

\n

In Vant 4, three components have been refactored, they are:

\n\n

The reason for refactoring these three components is that in the previous version, the API design of the Picker component had some unreasonable designs, which caused people to often encounter problems when using it, such as:

\n\n

To solve the above problems, we have refactored the Picker component in the v4 version, and also refactored the Area and DatetimePicker components derived from Picker. If you use these three components in your project, please read the documentation carefully and upgrade.

\n

Picker component refactoring

\n

Major changes

\n\n
\n

Please refer to Picker Component Documentation for detailed usage.

\n
\n

DatetimePicker component refactoring

\n

The DatetimePicker component is split into three subcomponents:

\n\n

At the same time, the TimePicker and DatePicker components are also refactored based on the new version of the Picker component, and some API designs are optimized.

\n

Major changes

\n

The following are the main API changes of TimePicker and DatePicker. For more details, please refer to TimePicker and DatePicker documentation.

\n\n
\n

Vant 4 no longer provides the old version of the DatetimePicker component. The PickerGroup component can be used to achieve more flexible and richer interactive effects. For specific usage, please refer to the PickerGroup component documentation.

\n
\n

Area component refactoring

\n

The Area component is encapsulated based on the Picker component, so this upgrade also refactors the internal logic of the Area component and optimizes some APIs.

\n

Major changes

\n\n
\n

Please refer to Area Component Documentation for detailed usage.

\n
\n

API tweaks

\n

Dialog calling method adjustment

\n

In Vant 3, Dialog is a function, and calling the function can quickly evoke the global Dialog component, and Dialog.Component is the Dialog component object, which is different from the usage of most components, which can easily lead to mistake.

\n

In order to be more intuitive, we adjusted the calling method of Dialog in Vant 4, renamed the Dialog() function to showDialog(), and let Dialog directly point to the component object.

\n
// Vant 3\nDialog(); // function call\nDialog.Component; // Component object\n\n// Vant 4\nshowDialog(); // function call\nDialog; // component object\n
\n

Since Dialog has become a component object, other methods mounted on Dialog have also been renamed. The mapping relationship between the old and new APIs is as follows:

\n
Dialog(); // -> showDialog()\nDialog.alert(); // -> showDialog()\nDialog.confirm(); // -> showConfirmDialog()\nDialog.close(); // -> closeDialog();\nDialog.setDefaultOptions(); // -> setDialogDefaultOptions()\nDialog.resetDefaultOptions(); // -> resetDialogDefaultOptions()\n
\n

Migration

\n

In order to facilitate the migration of old version code to v4, we provide a compatibility solution, you can use the Dialog object exported in @vant/compat to be compatible with the original code.

\n

Reference the Dialog method from @vant/compat:

\n
import { Dialog } from '@vant/compat';\n\nDialog();\nDialog.close();\n
\n

The Dialog exported in @vant/compat has exactly the same API and behavior as the Dialog in Vant 3, so you only need to modify the reference path of Dialog, and other codes can remain unchanged.

\n

After the project is upgraded to Vant v4, it is recommended to gradually replace it with the new showDialog and other methods in iterations, and remove the @vant/compat package.

\n

Toast calling method adjustment

\n

In Vant 4, the calling method of the Toast component has also been adjusted, which is consistent with the changes of the Dialog component:

\n
// Vant 3\nToast(); // function call\n\n// Vant 4\nshowToast(); // function call\nToast; // component object\n
\n

Other methods mounted on Toast have also been renamed, and the mapping relationship between the old and new APIs is as follows:

\n
Toast(); // -> showToast()\nToast.fail(); // -> showFailToast()\nToast.success(); // -> showSuccessToast()\nToast.loading(); // -> showLoadingToast()\nToast.clear(); // -> closeToast()\nToast.setDefaultOptions(); // -> setToastDefaultOptions()\nToast.resetDefaultOptions(); // -> resetToastDefaultOptions()\n
\n

At the same time, Vant 4 will no longer globally register the $toast method on the this object, which means that $toast will not be accessible on the this object.

\n

Migration

\n

In order to facilitate code migration, we provide a compatibility solution, you can use the Toast object exported in @vant/compat to be compatible with the original code.

\n
import { Toast } from '@vant/compat';\n\nToast();\nToast.clear();\n
\n

The Toast exported in @vant/compat has exactly the same API and behavior as Toast in Vant 3, so you only need to modify the reference path of Toast, and other codes can remain unchanged.

\n

Notify calling method adjustment

\n

In Vant 4, the calling method of the Notify component has also been adjusted, which is consistent with the changes of the Dialog component:

\n
// Vant 3\nNotify(); // function call\nNotify.Component; // component object\n\n// Vant 4\nshowNotify(); // function call\nNotify; // component object\n
\n

Other methods mounted on Notify have also been renamed, and the mapping relationship between the old and new APIs is as follows:

\n
Notify(); // -> showNotify()\nNotify.clear(); // -> closeNotify()\nNotify.setDefaultOptions(); // -> setNotifyDefaultOptions()\nNotify.resetDefaultOptions(); // -> resetNotifyDefaultOptions()\n
\n

At the same time, Vant 4 will no longer globally register the $notify method on the this object, which means that $notify will not be accessible on the this object.

\n

Migration

\n

In order to facilitate code migration, we provide a compatibility solution, you can use the Notify object exported in @vant/compat to be compatible with the original code.

\n
import { Notify } from '@vant/compat';\n\nNotify();\nNotify.clear();\n
\n

Notify exported in @vant/compat has exactly the same API and behavior as Notify in Vant 3, so you only need to modify the reference path of Notify, and other codes can remain unchanged.

\n

ImagePreview calling method adjustment

\n

In Vant 4, the calling method of the ImagePreview component has also been adjusted, which is consistent with the changes of the Dialog component:

\n
// Vant 3\nImagePreview(); // function call\nImagePreview.Component; // component object\n\n// Vant 4\nshowImagePreview(); // function call\nImagePreview; // component object\n
\n

Migration

\n

In order to facilitate code migration, we provide a compatibility solution, you can use the ImagePreview object exported in @vant/compat to be compatible with the original code.

\n
import { ImagePreview } from '@vant/compat';\n\nImagePreview();\n
\n

The ImagePreview exported in @vant/compat has exactly the same API and behavior as the ImagePreview in Vant 3, so you only need to modify the reference path of ImagePreview, and other codes can remain unchanged.

\n

Event naming adjustment

\n

Starting from Vant 4, all events are named in camelCase officially recommended by Vue.

\n
// Vant 3\nemit('click-input');\n\n// Vant 4\nemit('clickInput');\n
\n

This change does not affect the original template code, Vue will automatically format the event name in the template, so you don\'t need to make any changes.

\n
<!-- The following code works as usual without any changes -->\n<van-field @click-input="onClick" />\n
\n

If you use the Vant component in JSX, you need to adjust the monitored event name to camel case format, the original dash format will no longer take effect, and the new monitoring method is more in line with JSX\'s own specifications:

\n
// Vant 3\n<Field onClick-input={onClick} />\n\n// Vant 4\n<Field onClickInput={onClick} />\n
\n

Other API adjustments

\n

In Vant 4.0 version, the following APIs have been updated incompatible:

\n

AddressEdit

\n\n\n

Some adjustments have been made to the CSS style of Popup. If you have added some custom CSS styles to the Popup component, please check if this update affects the UI in the project.

\n\n
// Vant 3\n.van-popup --center {\n  left: 50%;\n  transform: translate3d(-50%, -50%, 0);\n}\n\n// Vant 4\n.van-popup --center {\n  left: 0;\n  right: 0;\n  width: fit-content;\n  max-width: calc(100vw - var(--van-padding-md) * 2);\n  margin: 0 auto;\n  transform: translateY(-50%);\n}\n
\n

Tabs

\n\n

Style adjustments

\n

Uniform primary color

\n

In the previous version, Vant components had two primary colors, some components used blue (#1989fa) as the primary color, and others used red (#ee0a24).

\n

To keep the colour specification consistent,, we have unified the primary color in Vant 4, and all components use blue as the primary color.

\n

The primary color of the following components has been changed from red to blue:

\n\n

Remove Less variables

\n

Currently, Vant already supports theme customization capabilities based on CSS variables, which is more flexible than Less customization. Therefore, Vant 4 will no longer provide Less-based theme customization.

\n

This means that Vant\'s npm package will no longer contain .less style source files, only the compiled .css style files will be provided.

\n

If your project is using an old version of Less theme customization, please use the ConfigProvider global configuration component to replace it.

\n

Simplify CSS variable names

\n

With code size and usability in mind, we have simplified the names of some CSS variables, and used shorter words in the variable names to reduce the code size.

\n

This upgrade includes the following variable name changes:

\n
animation-duration -> duration\nanimation-timing-function-enter -> ease-out\nanimation-timing-function-leave -> ease-in\nbackground-color -> background\nbackground-color-light -> background-2\nborder-radius -> radius\nborder-width-base -> border-width\nbox-shadow -> shadow\nfont-family -> font\nfont-weight-bold -> font-bold\nprice-integer-font -> price-font\ntext-link -> link\ntransition-duration -> duration\n
\n

Due to the large number of CSS variables involved, it is recommended that you perform a global match and replace in the code repository.

\n

For the ConfigProvider component, we have added the ConfigProviderThemeVars type definition to provide full type hints. In TypeScript code, you can use type hints to ensure that theme variables are substituted correctly.

\n
import type { ConfigProviderThemeVars } from 'vant';\n\nconst themeVars: ConfigProviderThemeVars = {\n  sliderBarHeight: '4px',\n};\n
\n
'},null,8,t))}}}]);