mirror of
https://gitee.com/vant-contrib/vant.git
synced 2025-04-06 03:57:59 +08:00
2 lines
28 KiB
JavaScript
2 lines
28 KiB
JavaScript
/*! For license information please see 5892.5215f2cd.js.LICENSE.txt */
|
|
(self.webpackChunk=self.webpackChunk||[]).push([["5892"],{8362:function(s,n,a){"use strict";a.r(n);var l=a("80681");let t=["innerHTML"];n.default={setup:()=>({html:""}),render:()=>((0,l.wg)(),(0,l.iD)("div",{class:"van-doc-markdown-body",innerHTML:'<h1>\u5FEB\u901F\u4E0A\u624B</h1>\n<div class="van-doc-card"><h3 id="jie-shao" tabindex="-1">\u4ECB\u7ECD</h3>\n<p>\u901A\u8FC7\u672C\u7AE0\u8282\u4F60\u53EF\u4EE5\u4E86\u89E3\u5230 Vant \u7684\u5B89\u88C5\u65B9\u6CD5\u548C\u57FA\u672C\u4F7F\u7528\u59FF\u52BF\u3002</p>\n</div><h2 id="an-zhuang" tabindex="-1">\u5B89\u88C5</h2>\n<div class="van-doc-card"><h3 id="tong-guo-npm-an-zhuang" tabindex="-1">\u901A\u8FC7 npm \u5B89\u88C5</h3>\n<p>\u5728\u73B0\u6709\u9879\u76EE\u4E2D\u4F7F\u7528 Vant \u65F6\uFF0C\u53EF\u4EE5\u901A\u8FC7 <code>npm</code> \u8FDB\u884C\u5B89\u88C5\uFF1A</p>\n<pre><code class="language-bash"><span class="hljs-comment"># Vue 3 \u9879\u76EE\uFF0C\u5B89\u88C5\u6700\u65B0\u7248 Vant</span>\nnpm i vant\n\n<span class="hljs-comment"># Vue 2 \u9879\u76EE\uFF0C\u5B89\u88C5 Vant 2</span>\nnpm i vant@latest-v2\n</code></pre>\n<p>\u5F53\u7136\uFF0C\u4F60\u4E5F\u53EF\u4EE5\u901A\u8FC7 <code>yarn</code>\u3001<code>pnpm</code> \u6216 <code>bun</code> \u8FDB\u884C\u5B89\u88C5\uFF1A</p>\n<pre><code class="language-bash"><span class="hljs-comment"># \u901A\u8FC7 yarn \u5B89\u88C5</span>\nyarn add vant\n\n<span class="hljs-comment"># \u901A\u8FC7 pnpm \u5B89\u88C5</span>\npnpm add vant\n\n<span class="hljs-comment"># \u901A\u8FC7 Bun \u5B89\u88C5</span>\nbun add vant\n</code></pre>\n</div><div class="van-doc-card"><h3 id="zai-xin-xiang-mu-zhong-shi-yong" tabindex="-1">\u5728\u65B0\u9879\u76EE\u4E2D\u4F7F\u7528</h3>\n<p>\u5982\u679C\u4F60\u9700\u8981\u65B0\u5EFA\u4E00\u4E2A\u9879\u76EE\uFF0C\u6211\u4EEC\u63A8\u8350\u4F7F\u7528 <a href="https://github.com/web-infra-dev/rsbuild" target="_blank">Rsbuild</a>\uFF0C<a href="https://cn.vitejs.dev/" target="_blank">Vite</a> \u6216 <a href="https://nuxt.com" target="_blank">Nuxt \u6846\u67B6</a>\u3002</p>\n<h4 id="rsbuild" tabindex="-1">Rsbuild</h4>\n<p>Rsbuild \u662F\u57FA\u4E8E Rspack \u7684\u6784\u5EFA\u5DE5\u5177\uFF0C\u7531 Vant \u4F5C\u8005\u5F00\u53D1\uFF0C\u5177\u5907\u4E00\u6D41\u7684\u6784\u5EFA\u901F\u5EA6\u548C\u5F00\u53D1\u4F53\u9A8C\uFF0C\u5BF9 Vant \u63D0\u4F9B\u7B2C\u4E00\u4F18\u5148\u7EA7\u652F\u6301\u3002</p>\n<p>\u4F60\u53EF\u4EE5\u901A\u8FC7\u4EE5\u4E0B\u547D\u4EE4\u521B\u5EFA\u4E00\u4E2A Rsbuild \u9879\u76EE\uFF1A</p>\n<pre><code class="language-bash">npm create rsbuild@latest\n</code></pre>\n<p>\u8BF7\u8BBF\u95EE <a href="https://github.com/web-infra-dev/rsbuild" target="_blank">Rsbuild \u4ED3\u5E93</a> \u4E86\u89E3\u66F4\u591A\u4FE1\u606F\u3002</p>\n<h4 id="shi-li-xiang-mu" tabindex="-1">\u793A\u4F8B\u9879\u76EE</h4>\n<p>\u4EE5\u4E0B\u662F Vant \u5B98\u65B9\u63D0\u4F9B\u7684\u4E00\u4E9B\u793A\u4F8B\u9879\u76EE\uFF0C\u4F60\u53EF\u4EE5\u514B\u9686\u8BE5\u9879\u76EE\uFF0C\u5E76\u76F4\u63A5\u62F7\u8D1D\u4EE3\u7801\u6765\u4F7F\u7528\u3002</p>\n<ul>\n<li><a href="https://github.com/vant-ui/vant-demo/tree/master/vant/rsbuild" target="_blank">vant-demo - rsbuild</a>\uFF1A\u4F7F\u7528 Vue 3\u3001Vant 4\u3001Rsbuild \u642D\u5EFA\u5E94\u7528</li>\n<li><a href="https://github.com/vant-ui/vant-demo/tree/master/vant/vite" target="_blank">vant-demo - vite</a>\uFF1A\u4F7F\u7528 Vue 3\u3001Vant 4\u3001Vite \u642D\u5EFA\u5E94\u7528</li>\n<li><a href="https://github.com/vant-ui/vant-demo/tree/master/vant/nuxt3" target="_blank">vant-demo - nuxt3</a>\uFF1A\u4F7F\u7528 Vue 3\u3001Nuxt 3\u3001Vant 4 \u642D\u5EFA\u5E94\u7528\u3002</li>\n</ul>\n</div><div class="van-doc-card"><h3 id="tong-guo-cdn-an-zhuang" tabindex="-1">\u901A\u8FC7 CDN \u5B89\u88C5</h3>\n<p>\u5982\u679C\u4F60\u53EA\u9700\u8981\u5F00\u53D1\u4E00\u4E2A\u7B80\u5355\u7684 HTML \u9875\u9762\uFF0C\u90A3\u4E48\u53EF\u4EE5\u76F4\u63A5\u5728 HTML \u6587\u4EF6\u4E2D\u5F15\u5165 CDN \u94FE\u63A5\uFF0C\u4E4B\u540E\u4F60\u53EF\u4EE5\u901A\u8FC7\u5168\u5C40\u53D8\u91CF <code>vant</code> \u8BBF\u95EE\u5230\u6240\u6709\u7EC4\u4EF6\u3002</p>\n<pre><code class="language-html"><span class="hljs-comment"><!-- \u5F15\u5165\u6837\u5F0F\u6587\u4EF6 --></span>\n<span class="hljs-tag"><<span class="hljs-name">link</span>\n <span class="hljs-attr">rel</span>=<span class="hljs-string">"stylesheet"</span>\n <span class="hljs-attr">href</span>=<span class="hljs-string">"https://fastly.jsdelivr.net/npm/vant@4/lib/index.css"</span>\n/></span>\n\n<span class="hljs-comment"><!-- \u5F15\u5165 Vue \u548C Vant \u7684 JS \u6587\u4EF6 --></span>\n<span class="hljs-tag"><<span class="hljs-name">script</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"https://fastly.jsdelivr.net/npm/vue@3"</span>></span><span class="hljs-tag"></<span class="hljs-name">script</span>></span>\n<span class="hljs-tag"><<span class="hljs-name">script</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"https://fastly.jsdelivr.net/npm/vant@4/lib/vant.min.js"</span>></span><span class="hljs-tag"></<span class="hljs-name">script</span>></span>\n\n<span class="hljs-tag"><<span class="hljs-name">script</span>></span><span class="language-javascript">\n <span class="hljs-comment">// \u5728 #app \u6807\u7B7E\u4E0B\u6E32\u67D3\u4E00\u4E2A\u6309\u94AE\u7EC4\u4EF6</span>\n <span class="hljs-keyword">const</span> app = <span class="hljs-title class_">Vue</span>.<span class="hljs-title function_">createApp</span>({\n <span class="hljs-attr">template</span>: <span class="hljs-string">`<van-button>\u6309\u94AE</van-button>`</span>,\n });\n app.<span class="hljs-title function_">use</span>(vant);\n\n <span class="hljs-comment">// \u901A\u8FC7 CDN \u5F15\u5165\u65F6\u4E0D\u4F1A\u81EA\u52A8\u6CE8\u518C Lazyload \u7EC4\u4EF6</span>\n <span class="hljs-comment">// \u53EF\u4EE5\u901A\u8FC7\u4E0B\u9762\u7684\u65B9\u5F0F\u624B\u52A8\u6CE8\u518C</span>\n app.<span class="hljs-title function_">use</span>(vant.<span class="hljs-property">Lazyload</span>);\n\n <span class="hljs-comment">// \u8C03\u7528\u5DE5\u5177\u51FD\u6570\uFF0C\u5F39\u51FA\u4E00\u4E2A Toast</span>\n vant.<span class="hljs-title function_">showToast</span>(<span class="hljs-string">'\u63D0\u793A'</span>);\n\n app.<span class="hljs-title function_">mount</span>(<span class="hljs-string">'#app'</span>);\n</span><span class="hljs-tag"></<span class="hljs-name">script</span>></span>\n</code></pre>\n<h4 id="mian-fei-cdn" tabindex="-1">\u514D\u8D39 CDN</h4>\n<p>\u4F60\u53EF\u4EE5\u901A\u8FC7\u4EE5\u4E0B\u514D\u8D39 CDN \u670D\u52A1\u6765\u4F7F\u7528 Vant:</p>\n<ul>\n<li><a href="https://www.jsdelivr.com/package/npm/vant" target="_blank">jsdelivr</a></li>\n<li><a href="https://cdnjs.com/libraries/vant" target="_blank">cdnjs</a></li>\n<li><a href="https://unpkg.com/" target="_blank">unpkg</a></li>\n</ul>\n<p>\u6CE8\u610F\uFF1A\u514D\u8D39 CDN \u4E00\u822C\u7528\u4E8E\u5236\u4F5C\u539F\u578B\u6216\u4E2A\u4EBA\u5C0F\u578B\u9879\u76EE\uFF0C\u4E0D\u63A8\u8350\u5728\u4F01\u4E1A\u751F\u4EA7\u73AF\u5883\u4E2D\u4F7F\u7528\u514D\u8D39 CDN\u3002</p>\n<p>\u5BF9\u4E8E\u4F01\u4E1A\u5F00\u53D1\u8005\uFF0C\u5EFA\u8BAE\u4F7F\u7528\u4EE5\u4E0B\u65B9\u5F0F\uFF1A</p>\n<ul>\n<li>\u901A\u8FC7 npm \u5F15\u5165\uFF0C\u5E76\u901A\u8FC7\u6784\u5EFA\u5DE5\u5177\u8FDB\u884C\u6253\u5305</li>\n<li>\u4E0B\u8F7D\u5BF9\u5E94\u6587\u4EF6\uFF0C\u5E76\u6258\u7BA1\u5728\u4F60\u81EA\u5DF1\u7684\u670D\u52A1\u5668\u6216 CDN \u4E0A</li>\n</ul>\n</div><h2 id="shi-li" tabindex="-1">\u793A\u4F8B</h2>\n<div class="van-doc-card"><h3 id="shi-li-gong-cheng" tabindex="-1">\u793A\u4F8B\u5DE5\u7A0B</h3>\n<p>\u6211\u4EEC\u63D0\u4F9B\u4E86\u4E30\u5BCC\u7684<a href="https://github.com/vant-ui/vant-demo" target="_blank">\u793A\u4F8B\u5DE5\u7A0B</a>\uFF0C\u901A\u8FC7\u793A\u4F8B\u5DE5\u7A0B\u4F60\u53EF\u4EE5\u4E86\u89E3\u5982\u4E0B\u5185\u5BB9\uFF1A</p>\n<ul>\n<li>\u57FA\u4E8E Rsbuild \u548C Vant \u642D\u5EFA\u5E94\u7528</li>\n<li>\u57FA\u4E8E Vite \u548C Vant \u642D\u5EFA\u5E94\u7528</li>\n<li>\u57FA\u4E8E Nuxt \u548C Vant \u642D\u5EFA\u5E94\u7528</li>\n<li>\u57FA\u4E8E Vue CLI \u548C Vant \u642D\u5EFA\u5E94\u7528</li>\n<li>\u914D\u7F6E\u6309\u9700\u5F15\u5165\u7EC4\u4EF6</li>\n<li>\u914D\u7F6E\u57FA\u4E8E Rem \u7684\u9002\u914D\u65B9\u6848</li>\n<li>\u914D\u7F6E\u57FA\u4E8E Viewport \u7684\u9002\u914D\u65B9\u6848</li>\n<li>\u914D\u7F6E\u57FA\u4E8E TypeScript \u7684\u5DE5\u7A0B</li>\n</ul>\n</div><h2 id="yin-ru-zu-jian" tabindex="-1">\u5F15\u5165\u7EC4\u4EF6</h2>\n<div class="van-doc-card"><h3 id="fang-fa-yi.-chang-gui-yong-fa" tabindex="-1">\u65B9\u6CD5\u4E00. \u5E38\u89C4\u7528\u6CD5</h3>\n<p>\u4E0B\u9762\u662F\u4F7F\u7528 Vant \u7EC4\u4EF6\u7684\u7528\u6CD5\u793A\u4F8B\uFF1A</p>\n<pre><code class="language-js"><span class="hljs-keyword">import</span> { createApp } <span class="hljs-keyword">from</span> <span class="hljs-string">'vue'</span>;\n<span class="hljs-comment">// 1. \u5F15\u5165\u4F60\u9700\u8981\u7684\u7EC4\u4EF6</span>\n<span class="hljs-keyword">import</span> { <span class="hljs-title class_">Button</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'vant'</span>;\n<span class="hljs-comment">// 2. \u5F15\u5165\u7EC4\u4EF6\u6837\u5F0F</span>\n<span class="hljs-keyword">import</span> <span class="hljs-string">'vant/lib/index.css'</span>;\n\n<span class="hljs-keyword">const</span> app = <span class="hljs-title function_">createApp</span>();\n\n<span class="hljs-comment">// 3. \u6CE8\u518C\u4F60\u9700\u8981\u7684\u7EC4\u4EF6</span>\napp.<span class="hljs-title function_">use</span>(<span class="hljs-title class_">Button</span>);\n</code></pre>\n<p>Vant \u652F\u6301\u591A\u79CD\u7EC4\u4EF6\u6CE8\u518C\u65B9\u5F0F\uFF0C\u9664\u4E86\u5728 app \u4E0A\u5168\u5C40\u6CE8\u518C\u7EC4\u4EF6\uFF0C\u4F60\u4E5F\u53EF\u4EE5\u9009\u62E9\u5176\u4ED6\u7684\u65B9\u5F0F\uFF0C\u6BD4\u5982\u5C40\u90E8\u6CE8\u518C\uFF0C\u8BE6\u89C1 <a href="#/zh-CN/advanced-usage#zu-jian-zhu-ce" target="_blank">\u7EC4\u4EF6\u6CE8\u518C</a> \u7AE0\u8282\u3002</p>\n<blockquote>\n<p>\u63D0\u793A\uFF1AVant \u9ED8\u8BA4\u652F\u6301 Tree Shaking\uFF0C\u56E0\u6B64\u4F60\u4E0D\u9700\u8981\u914D\u7F6E\u4EFB\u4F55\u63D2\u4EF6\uFF0C\u901A\u8FC7 Tree Shaking \u5373\u53EF\u79FB\u9664\u4E0D\u9700\u8981\u7684 JS \u4EE3\u7801\uFF0C\u4F46 CSS \u6837\u5F0F\u65E0\u6CD5\u901A\u8FC7\u8FD9\u79CD\u65B9\u5F0F\u4F18\u5316\uFF0C\u5982\u679C\u9700\u8981\u6309\u9700\u5F15\u5165 CSS \u6837\u5F0F\uFF0C\u8BF7\u53C2\u8003\u4E0B\u9762\u7684\u65B9\u6CD5\u4E8C\u3002</p>\n</blockquote>\n</div><div class="van-doc-card"><h3 id="fang-fa-er.-an-xu-yin-ru-zu-jian-yang-shi" tabindex="-1">\u65B9\u6CD5\u4E8C. \u6309\u9700\u5F15\u5165\u7EC4\u4EF6\u6837\u5F0F</h3>\n<p>\u5728\u57FA\u4E8E Rsbuild\u3001Vite\u3001webpack \u6216 vue-cli \u7684\u9879\u76EE\u4E2D\u4F7F\u7528 Vant \u65F6\uFF0C\u53EF\u4EE5\u4F7F\u7528 <a href="https://github.com/unplugin/unplugin-vue-components" target="_blank">unplugin-vue-components</a> \u63D2\u4EF6\uFF0C\u5B83\u53EF\u4EE5\u81EA\u52A8\u5F15\u5165\u7EC4\u4EF6\u3002</p>\n<p>Vant \u5B98\u65B9\u57FA\u4E8E <code>unplugin-vue-components</code> \u63D0\u4F9B\u4E86\u81EA\u52A8\u5BFC\u5165\u6837\u5F0F\u7684\u89E3\u6790\u5668 <a href="https://github.com/youzan/vant/tree/main/packages/vant-auto-import-resolver" target="_blank">@vant/auto-import-resolver</a>\uFF0C\u4E24\u8005\u53EF\u4EE5\u914D\u5408\u4F7F\u7528\u3002</p>\n<p>\u76F8\u6BD4\u4E8E\u5E38\u89C4\u7528\u6CD5\uFF0C\u8FD9\u79CD\u65B9\u5F0F\u53EF\u4EE5\u6309\u9700\u5F15\u5165\u7EC4\u4EF6\u7684 CSS \u6837\u5F0F\uFF0C\u4ECE\u800C\u51CF\u5C11\u4E00\u90E8\u5206\u4EE3\u7801\u4F53\u79EF\uFF0C\u4F46\u4F7F\u7528\u8D77\u6765\u4F1A\u53D8\u5F97\u7E41\u7410\u4E00\u4E9B\u3002\u5982\u679C\u4E1A\u52A1\u5BF9 CSS \u7684\u4F53\u79EF\u8981\u6C42\u4E0D\u662F\u7279\u522B\u6781\u81F4\uFF0C\u6211\u4EEC\u63A8\u8350\u4F7F\u7528\u66F4\u7B80\u4FBF\u7684\u5E38\u89C4\u7528\u6CD5\u3002</p>\n<h4 id="1.-an-zhuang-cha-jian" tabindex="-1">1. \u5B89\u88C5\u63D2\u4EF6</h4>\n<pre><code class="language-bash"><span class="hljs-comment"># \u901A\u8FC7 npm \u5B89\u88C5</span>\nnpm i @vant/auto-import-resolver unplugin-vue-components unplugin-auto-import -D\n\n<span class="hljs-comment"># \u901A\u8FC7 yarn \u5B89\u88C5</span>\nyarn add @vant/auto-import-resolver unplugin-vue-components unplugin-auto-import -D\n\n<span class="hljs-comment"># \u901A\u8FC7 pnpm \u5B89\u88C5</span>\npnpm add @vant/auto-import-resolver unplugin-vue-components unplugin-auto-import -D\n\n<span class="hljs-comment"># \u901A\u8FC7 bun \u5B89\u88C5</span>\nbun add @vant/auto-import-resolver unplugin-vue-components unplugin-auto-import -D\n</code></pre>\n<h4 id="2.-pei-zhi-cha-jian" tabindex="-1">2. \u914D\u7F6E\u63D2\u4EF6</h4>\n<p>\u5982\u679C\u662F\u57FA\u4E8E <a href="https://github.com/web-infra-dev/rsbuild" target="_blank">Rsbuild</a> \u7684\u9879\u76EE\uFF0C\u5728 <code>rsbuild.config.js</code> \u6587\u4EF6\u4E2D\u914D\u7F6E\u63D2\u4EF6\uFF1A</p>\n<pre><code class="language-js"><span class="hljs-keyword">import</span> { defineConfig } <span class="hljs-keyword">from</span> <span class="hljs-string">'@rsbuild/core'</span>;\n<span class="hljs-keyword">import</span> { pluginVue } <span class="hljs-keyword">from</span> <span class="hljs-string">'@rsbuild/plugin-vue'</span>;\n<span class="hljs-keyword">import</span> <span class="hljs-title class_">AutoImport</span> <span class="hljs-keyword">from</span> <span class="hljs-string">'unplugin-auto-import/rspack'</span>;\n<span class="hljs-keyword">import</span> <span class="hljs-title class_">Components</span> <span class="hljs-keyword">from</span> <span class="hljs-string">'unplugin-vue-components/rspack'</span>;\n<span class="hljs-keyword">import</span> { <span class="hljs-title class_">VantResolver</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'@vant/auto-import-resolver'</span>;\n\n<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-title function_">defineConfig</span>({\n <span class="hljs-attr">plugins</span>: [<span class="hljs-title function_">pluginVue</span>()],\n <span class="hljs-attr">tools</span>: {\n <span class="hljs-attr">rspack</span>: {\n <span class="hljs-attr">plugins</span>: [\n <span class="hljs-title class_">AutoImport</span>({\n <span class="hljs-attr">resolvers</span>: [<span class="hljs-title class_">VantResolver</span>()],\n }),\n <span class="hljs-title class_">Components</span>({\n <span class="hljs-attr">resolvers</span>: [<span class="hljs-title class_">VantResolver</span>()],\n }),\n ],\n },\n },\n});\n</code></pre>\n<p>\u5982\u679C\u662F\u57FA\u4E8E Vite \u7684\u9879\u76EE\uFF0C\u5728 <code>vite.config.js</code> \u6587\u4EF6\u4E2D\u914D\u7F6E\u63D2\u4EF6\uFF1A</p>\n<pre><code class="language-js"><span class="hljs-keyword">import</span> vue <span class="hljs-keyword">from</span> <span class="hljs-string">'@vitejs/plugin-vue'</span>;\n<span class="hljs-keyword">import</span> <span class="hljs-title class_">AutoImport</span> <span class="hljs-keyword">from</span> <span class="hljs-string">'unplugin-auto-import/vite'</span>;\n<span class="hljs-keyword">import</span> <span class="hljs-title class_">Components</span> <span class="hljs-keyword">from</span> <span class="hljs-string">'unplugin-vue-components/vite'</span>;\n<span class="hljs-keyword">import</span> { <span class="hljs-title class_">VantResolver</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'@vant/auto-import-resolver'</span>;\n\n<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> {\n <span class="hljs-attr">plugins</span>: [\n <span class="hljs-title function_">vue</span>(),\n <span class="hljs-title class_">AutoImport</span>({\n <span class="hljs-attr">resolvers</span>: [<span class="hljs-title class_">VantResolver</span>()],\n }),\n <span class="hljs-title class_">Components</span>({\n <span class="hljs-attr">resolvers</span>: [<span class="hljs-title class_">VantResolver</span>()],\n }),\n ],\n};\n</code></pre>\n<p>\u5982\u679C\u662F\u57FA\u4E8E vue-cli \u7684\u9879\u76EE\uFF0C\u5728 <code>vue.config.js</code> \u6587\u4EF6\u4E2D\u914D\u7F6E\u63D2\u4EF6\uFF1A</p>\n<pre><code class="language-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">VantResolver</span> } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'@vant/auto-import-resolver'</span>);\n<span class="hljs-keyword">const</span> <span class="hljs-title class_">AutoImport</span> = <span class="hljs-built_in">require</span>(<span class="hljs-string">'unplugin-auto-import/webpack'</span>);\n<span class="hljs-keyword">const</span> <span class="hljs-title class_">Components</span> = <span class="hljs-built_in">require</span>(<span class="hljs-string">'unplugin-vue-components/webpack'</span>);\n\n<span class="hljs-variable language_">module</span>.<span class="hljs-property">exports</span> = {\n <span class="hljs-attr">configureWebpack</span>: {\n <span class="hljs-attr">plugins</span>: [\n <span class="hljs-comment">// \u5F53 unplugin-vue-components \u7248\u672C\u5C0F\u4E8E 0.26.0 \u65F6\uFF0C\u4F7F\u7528\u4EE5\u4E0B\u5199\u6CD5</span>\n <span class="hljs-title class_">AutoImport</span>({ <span class="hljs-attr">resolvers</span>: [<span class="hljs-title class_">VantResolver</span>()] }),\n <span class="hljs-title class_">Components</span>({ <span class="hljs-attr">resolvers</span>: [<span class="hljs-title class_">VantResolver</span>()] }),\n <span class="hljs-comment">//\u5F53 unplugin-vue-components \u7248\u672C\u5927\u4E8E\u7B49\u4E8E 0.26.0 \u65F6\uFF0C\u4F7F\u7528\u4EE5\u4E0B\u5199\u6CD5</span>\n <span class="hljs-title class_">AutoImport</span>.<span class="hljs-title function_">default</span>({ <span class="hljs-attr">resolvers</span>: [<span class="hljs-title class_">VantResolver</span>()] }),\n <span class="hljs-title class_">Components</span>.<span class="hljs-title function_">default</span>({ <span class="hljs-attr">resolvers</span>: [<span class="hljs-title class_">VantResolver</span>()] }),\n ],\n },\n};\n</code></pre>\n<p>\u5982\u679C\u662F\u57FA\u4E8E webpack \u7684\u9879\u76EE\uFF0C\u5728 <code>webpack.config.js</code> \u6587\u4EF6\u4E2D\u914D\u7F6E\u63D2\u4EF6\uFF1A</p>\n<pre><code class="language-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">VantResolver</span> } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'@vant/auto-import-resolver'</span>);\n<span class="hljs-keyword">const</span> <span class="hljs-title class_">AutoImport</span> = <span class="hljs-built_in">require</span>(<span class="hljs-string">'unplugin-auto-import/webpack'</span>);\n<span class="hljs-keyword">const</span> <span class="hljs-title class_">Components</span> = <span class="hljs-built_in">require</span>(<span class="hljs-string">'unplugin-vue-components/webpack'</span>);\n\n<span class="hljs-variable language_">module</span>.<span class="hljs-property">exports</span> = {\n <span class="hljs-attr">plugins</span>: [\n <span class="hljs-comment">// \u5F53 unplugin-vue-components \u7248\u672C\u5C0F\u4E8E 0.26.0 \u65F6\uFF0C\u4F7F\u7528\u4EE5\u4E0B\u5199\u6CD5</span>\n <span class="hljs-title class_">AutoImport</span>({ <span class="hljs-attr">resolvers</span>: [<span class="hljs-title class_">VantResolver</span>()] }),\n <span class="hljs-title class_">Components</span>({ <span class="hljs-attr">resolvers</span>: [<span class="hljs-title class_">VantResolver</span>()] }),\n <span class="hljs-comment">//\u5F53 unplugin-vue-components \u7248\u672C\u5927\u4E8E\u7B49\u4E8E 0.26.0 \u65F6\uFF0C\u4F7F\u7528\u4EE5\u4E0B\u5199\u6CD5</span>\n <span class="hljs-title class_">AutoImport</span>.<span class="hljs-title function_">default</span>({ <span class="hljs-attr">resolvers</span>: [<span class="hljs-title class_">VantResolver</span>()] }),\n <span class="hljs-title class_">Components</span>.<span class="hljs-title function_">default</span>({ <span class="hljs-attr">resolvers</span>: [<span class="hljs-title class_">VantResolver</span>()] }),\n ],\n};\n</code></pre>\n<h4 id="3.-shi-yong-zu-jian-he-api" tabindex="-1">3. \u4F7F\u7528\u7EC4\u4EF6\u548C API</h4>\n<p>\u5B8C\u6210\u4EE5\u4E0A\u4E24\u6B65\uFF0C\u5C31\u53EF\u4EE5\u76F4\u63A5\u5728\u6A21\u677F\u4E2D\u4F7F\u7528 Vant \u7EC4\u4EF6\u4E86\uFF0C<code>unplugin-vue-components</code> \u4F1A\u89E3\u6790\u6A21\u677F\u5E76\u81EA\u52A8\u6CE8\u518C\u5BF9\u5E94\u7684\u7EC4\u4EF6, <code>@vant/auto-import-resolver</code> \u4F1A\u81EA\u52A8\u5F15\u5165\u5BF9\u5E94\u7684\u7EC4\u4EF6\u6837\u5F0F\u3002</p>\n<pre><code class="language-html"><span class="hljs-tag"><<span class="hljs-name">template</span>></span>\n <span class="hljs-tag"><<span class="hljs-name">van-button</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"primary"</span> /></span>\n<span class="hljs-tag"></<span class="hljs-name">template</span>></span>\n</code></pre>\n<p><code>unplugin-auto-import</code> \u4F1A\u81EA\u52A8\u5BFC\u5165\u5BF9\u5E94\u7684 Vant API \u4EE5\u53CA\u6837\u5F0F\u3002</p>\n<pre><code class="language-html"><span class="hljs-tag"><<span class="hljs-name">script</span>></span><span class="language-javascript">\n<span class="hljs-title function_">showToast</span>(<span class="hljs-string">'No need to import showToast'</span>)\n</span><span class="hljs-tag"></<span class="hljs-name">script</span>></span>\n</code></pre>\n<h4 id="shi-yong-ti-shi" tabindex="-1">\u4F7F\u7528\u63D0\u793A</h4>\n<ul>\n<li>\u8BF7\u907F\u514D\u540C\u65F6\u4F7F\u7528\u300C\u5168\u91CF\u5F15\u5165\u300D\u548C\u300C\u6309\u9700\u5F15\u5165\u300D\u8FD9\u4E24\u79CD\u5F15\u5165\u65B9\u5F0F\uFF0C\u5426\u5219\u4F1A\u5BFC\u81F4\u4EE3\u7801\u91CD\u590D\u3001\u6837\u5F0F\u9519\u4E71\u7B49\u95EE\u9898\u3002</li>\n<li>\u5728\u4F7F\u7528\u8FC7\u7A0B\u4E2D\uFF0C\u5982\u679C\u4F60\u9047\u5230\u7EC4\u4EF6\u4E0D\u80FD\u5BFC\u5165\u7684\u95EE\u9898\uFF0C\u56E0\u4E3A unplugin-vue-components \u5E76\u4E0D\u662F Vant \u5B98\u65B9\u7EF4\u62A4\u7684\u63D2\u4EF6\uFF0C\u6240\u4EE5\u5EFA\u8BAE\u5230 <a href="https://github.com/unplugin/unplugin-vue-components" target="_blank">unplugin/unplugin-vue-components</a> \u4ED3\u5E93\u4E0B\u53CD\u9988\u3002</li>\n<li>\u5F53 <code>unplugin-vue-components</code> \u7684\u7248\u672C\u53F7 >= 0.26.0 \u65F6\uFF0C\u5BF9\u4E8E <code>webpack</code>\u3001<code>vue-cli</code> \u548C <code>rspack</code>\uFF0C\u4F60\u9700\u8981\u4F7F\u7528 <code>ComponentsPlugin.default</code> \u8FDB\u884C\u6CE8\u518C\u3002</li>\n<li><code>@vant/auto-import-resolver</code> \u63D0\u4F9B\u4E86\u4E00\u4E9B\u914D\u7F6E\u9879\uFF0C\u8BF7\u53C2\u8003 <a href="https://github.com/youzan/vant/tree/main/packages/vant-auto-import-resolver" target="_blank">README \u6587\u6863</a> \u6765\u4E86\u89E3\u66F4\u591A\u3002</li>\n<li>\u5982\u679C\u662F\u6837\u5F0F\u4E0D\u751F\u6548\u7684\u76F8\u5173\u95EE\u9898\uFF0C\u4F60\u53EF\u4EE5\u5728 Vant \u4ED3\u5E93\u4E0B\u53CD\u9988\u3002</li>\n</ul>\n</div><h2 id="zai-kuang-jia-zhong-shi-yong" tabindex="-1">\u5728\u6846\u67B6\u4E2D\u4F7F\u7528</h2>\n<div class="van-doc-card"><h3 id="zai-nuxt-3-zhong-shi-yong" tabindex="-1">\u5728 Nuxt 3 \u4E2D\u4F7F\u7528</h3>\n<p>\u5728 Nuxt 3 \u4E2D\u4F7F\u7528 Vant \u65F6\uFF0C\u53EF\u4EE5\u4F7F\u7528 <a href="https://github.com/vant-ui/vant-nuxt" target="_blank">vant-nuxt</a> \u6A21\u5757\uFF0C\u5B83\u53EF\u4EE5\u81EA\u52A8\u5F15\u5165\u7EC4\u4EF6\uFF0C\u5E76\u6309\u9700\u5F15\u5165\u7684\u6837\u5F0F\uFF08\u5305\u62EC\u51FD\u6570\u7EC4\u4EF6\uFF09\u3002</p>\n<h4 id="1.-an-zhuang-mo-kuai" tabindex="-1">1. \u5B89\u88C5\u6A21\u5757</h4>\n<pre><code class="language-bash"><span class="hljs-comment"># \u901A\u8FC7 npm \u5B89\u88C5</span>\nnpm i @vant/nuxt -D\n\n<span class="hljs-comment"># \u901A\u8FC7 yarn \u5B89\u88C5</span>\nyarn add @vant/nuxt -D\n\n<span class="hljs-comment"># \u901A\u8FC7 pnpm \u5B89\u88C5</span>\npnpm add @vant/nuxt -D\n\n<span class="hljs-comment"># \u901A\u8FC7 Bun \u5B89\u88C5</span>\nbun add @vant/nuxt -D\n</code></pre>\n<h4 id="2.-zeng-jia-mo-kuai" tabindex="-1">2. \u589E\u52A0\u6A21\u5757</h4>\n<p>\u5728 <code>nuxt.config.js</code> \u6587\u4EF6\u4E2D\u589E\u52A0\u6A21\u5757\uFF1A</p>\n<pre><code class="language-js"><span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-title function_">defineNuxtConfig</span>({\n <span class="hljs-attr">modules</span>: [<span class="hljs-string">'@vant/nuxt'</span>],\n});\n</code></pre>\n<h4 id="3.-shi-yong-zu-jian" tabindex="-1">3. \u4F7F\u7528\u7EC4\u4EF6</h4>\n<p>\u5B8C\u6210\u4EE5\u4E0A\u4E24\u6B65\uFF0C\u5C31\u53EF\u4EE5\u76F4\u63A5\u5728\u6A21\u677F\u4E2D\u4F7F\u7528 Vant \u7EC4\u4EF6\u4E86\u3002\u524D\u5F80 <a href="https://nuxt.com/docs/guide/directory-structure/components" target="_blank">Nuxt \u6587\u6863</a> \u4E86\u89E3\u66F4\u591A\u3002</p>\n<pre><code class="language-html"><span class="hljs-tag"><<span class="hljs-name">template</span>></span>\n <span class="hljs-tag"><<span class="hljs-name">van-button</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"primary"</span> @<span class="hljs-attr">click</span>=<span class="hljs-string">"showToast('toast')"</span>></span>button<span class="hljs-tag"></<span class="hljs-name">van-button</span>></span>\n <span class="hljs-tag"><<span class="hljs-name">VanButton</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"success"</span> @<span class="hljs-attr">click</span>=<span class="hljs-string">"showNotify('notify')"</span>></span>button<span class="hljs-tag"></<span class="hljs-name">VanButton</span>></span>\n <span class="hljs-tag"><<span class="hljs-name">LazyVanButton</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"default"</span>></span>lazy button<span class="hljs-tag"></<span class="hljs-name">LazyVanButton</span>></span>\n<span class="hljs-tag"></<span class="hljs-name">template</span>></span>\n</code></pre>\n</div><h2 id="qian-yi-ti-shi" tabindex="-1">\u8FC1\u79FB\u63D0\u793A</h2>\n<div class="van-doc-card"><h3 id="yi-chu-babel-plugin-import" tabindex="-1">\u79FB\u9664 babel-plugin-import</h3>\n<p>\u4ECE Vant 4.0 \u7248\u672C\u5F00\u59CB\uFF0C\u5C06\u4E0D\u518D\u652F\u6301 <code>babel-plugin-import</code>\uFF0C\u8BF7\u79FB\u9664\u9879\u76EE\u4E2D\u4F9D\u8D56\u7684 <code>babel-plugin-import</code> \u63D2\u4EF6\u3002</p>\n<p>\u53EA\u9700\u8981\u5220\u9664 <code>babel.config.js</code> \u4E2D\u7684\u4EE5\u4E0B\u4EE3\u7801\u5373\u53EF\uFF1A</p>\n<pre><code class="language-diff">module.exports = {\n plugins: [\n<span class="hljs-deletion">- ['import', {</span>\n<span class="hljs-deletion">- libraryName: 'vant',</span>\n<span class="hljs-deletion">- libraryDirectory: 'es',</span>\n<span class="hljs-deletion">- style: true</span>\n<span class="hljs-deletion">- }, 'vant']</span>\n ]\n};\n</code></pre>\n<h4 id="shou-yi" tabindex="-1">\u6536\u76CA</h4>\n<p>\u79FB\u9664 <code>babel-plugin-import</code> \u6709\u4EE5\u4E0B\u6536\u76CA\uFF1A</p>\n<ul>\n<li>\u4E0D\u518D\u5F3A\u4F9D\u8D56 babel\uFF0C\u9879\u76EE\u53EF\u4EE5\u4F7F\u7528 esbuild\u3001swc \u7B49\u66F4\u9AD8\u6548\u7684\u7F16\u8BD1\u5DE5\u5177\uFF0C\u5927\u5E45\u5EA6\u63D0\u5347\u7F16\u8BD1\u6548\u7387\u3002</li>\n<li>\u4E0D\u518D\u53D7\u5230 <code>babel-plugin-import</code> \u7684 import \u5199\u6CD5\u9650\u5236\uFF0C\u53EF\u4EE5\u4ECE vant \u4E2D\u5BFC\u5165\u9664\u4E86\u7EC4\u4EF6\u4EE5\u5916\u7684\u5176\u4ED6\u5185\u5BB9\uFF0C\u6BD4\u5982 Vant 4 \u4E2D\u65B0\u589E\u7684 <code>showToast</code> \u7B49\u65B9\u6CD5\u3002</li>\n</ul>\n<pre><code class="language-ts"><span class="hljs-keyword">import</span> { showToast, showDialog } <span class="hljs-keyword">from</span> <span class="hljs-string">'vant'</span>;\n</code></pre>\n</div>'},null,8,t))}}}]); |