{"id":298567,"date":"2024-05-20T16:55:50","date_gmt":"2024-05-20T08:55:50","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/298567.html"},"modified":"2024-05-20T16:55:57","modified_gmt":"2024-05-20T08:55:57","slug":"vue3-%e8%87%aa%e5%ae%9a%e4%b9%89%e7%bb%84%e4%bb%b6%e5%a6%82%e4%bd%95%e5%8f%91%e5%b8%83%e5%88%b0-npm","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/298567.html","title":{"rendered":"vue3 \u81ea\u5b9a\u4e49\u7ec4\u4ef6\u5982\u4f55\u53d1\u5e03\u5230 npm"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/27150527\/8295460f-668a-4e13-b213-2cb74be2bfae.webp\" alt=\"vue3 \u81ea\u5b9a\u4e49\u7ec4\u4ef6\u5982\u4f55\u53d1\u5e03\u5230 npm\" \/><\/p>\n<p><p><strong>Vue3 \u81ea\u5b9a\u4e49\u7ec4\u4ef6\u53d1\u5e03\u5230 npm<\/strong> \u7684\u57fa\u672c\u6b65\u9aa4\u5305\u62ec\uff1a\u521b\u5efa\u4e00\u4e2a\u7ec4\u4ef6\u3001\u7f16\u5199\u8be5\u7ec4\u4ef6\u7684\u6e90\u4ee3\u7801\u548c\u6837\u5f0f\u3001\u7f16\u5199\u7ec4\u4ef6\u7684\u4f7f\u7528\u6587\u6863\u3001\u914d\u7f6e\u5305\u7684\u76f8\u5173\u4fe1\u606f\u3001\u6d4b\u8bd5\u7ec4\u4ef6\u7684\u529f\u80fd\u3001\u901a\u8fc7\u6253\u5305\u5de5\u5177\u5bf9\u7ec4\u4ef6\u8fdb\u884c\u6784\u5efa\u3001\u53d1\u5e03\u5230 npm \u6ce8\u518c\u8868\u3002<\/p>\n<\/p>\n<p><p>\u5728 Vue3 \u4e2d\uff0c\u81ea\u5b9a\u4e49\u7ec4\u4ef6\u6700\u901a\u5e38\u7684\u53d1\u5e03\u6b65\u9aa4\u662f\u4f7f\u7528 Vue CLI \u6216 Vite \u8fd9\u7c7b\u73b0\u4ee3\u5316\u7684\u524d\u7aef\u5de5\u5177\u6765\u521b\u5efa\u548c\u6253\u5305\u7ec4\u4ef6\u3002<strong>\u6700\u5173\u952e\u7684\u6b65\u9aa4\u662f\u786e\u4fdd\u4f60\u7684\u7ec4\u4ef6\u662f\u7ecf\u8fc7\u5145\u5206\u6d4b\u8bd5\u548c\u6587\u6863\u64b0\u5199\u7684<\/strong>\uff0c\u8fd9\u6837\u5176\u4ed6\u5f00\u53d1\u8005\u624d\u80fd\u66f4\u5bb9\u6613\u5730\u4f7f\u7528\u4f60\u7684\u7ec4\u4ef6\u3002\u63a5\u4e0b\u6765\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u521b\u5efa\u7ec4\u4ef6<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u4f60\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u65b0\u7684 Vue3 \u9879\u76ee\u6216\u5728\u73b0\u6709\u9879\u76ee\u4e2d\u521b\u5efa\u4e00\u4e2a\u65b0\u7ec4\u4ef6\u3002\u5047\u8bbe\u4f60\u662f\u4ece\u96f6\u5f00\u59cb\u521b\u5efa\uff0c\u53ef\u4ee5\u4f7f\u7528 Vue CLI \u6216 Vite \u8fd9\u6837\u7684\u5de5\u5177\u6765\u521d\u59cb\u5316\u9879\u76ee\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-shell\">npm install -g @vue\/cli<\/p>\n<h2><strong>\u6216\u8005<\/strong><\/h2>\n<p>npm install -g create-vite<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u521b\u5efa\u9879\u76ee\u540e\uff0c\u7f16\u5199\u4f60\u7684\u7ec4\u4ef6\u3002\u7ec4\u4ef6\u5e94\u8be5\u662f\u5355\u6587\u4ef6\u7ec4\u4ef6\uff08Single File Component, SFC\uff09\uff0c\u5373\u4e00\u4e2a<code>.vue<\/code>\u6587\u4ef6\u4e2d\u5305\u542b\u4e86\u6a21\u677f\uff08template\uff09\u3001\u811a\u672c\uff08script\uff09\u548c\u6837\u5f0f\uff08style\uff09\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u7f16\u5199\u6e90\u4ee3\u7801\u548c\u6837\u5f0f<\/h3>\n<\/p>\n<p><p>\u7ec4\u4ef6\u7684\u6e90\u4ee3\u7801\u5e94\u6309\u7167 Vue3 \u7684\u7ec4\u4ef6\u89c4\u8303\u6765\u7f16\u5199\u3002\u786e\u4fdd\u4f60\u7684\u7ec4\u4ef6\u5145\u5206\u5229\u7528\u4e86 Vue3 \u63d0\u4f9b\u7684\u54cd\u5e94\u5f0f API \u548c\u7ec4\u5408\u5f0f API\u3002\u6837\u5f0f\u53ef\u4ee5\u5185\u8054\u5199\u5728\u7ec4\u4ef6\u6587\u4ef6\u4e2d\uff0c\u4e5f\u53ef\u4ee5\u4f5c\u4e3a\u5355\u72ec\u7684\u6587\u4ef6\u5f15\u5165\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-vue\">&lt;template&gt;<\/p>\n<p>  &lt;div class=&quot;my-component&quot;&gt;<\/p>\n<p>    &lt;!-- \u7ec4\u4ef6\u6a21\u677f --&gt;<\/p>\n<p>  &lt;\/div&gt;<\/p>\n<p>&lt;\/template&gt;<\/p>\n<p>&lt;script setup&gt;<\/p>\n<p>import { ref } from &#039;vue&#039;;<\/p>\n<p>const count = ref(0);<\/p>\n<p>function increment() {<\/p>\n<p>  count.value++;<\/p>\n<p>}<\/p>\n<p>&lt;\/script&gt;<\/p>\n<p>&lt;style scoped&gt;<\/p>\n<p>.my-component {<\/p>\n<p>  \/* \u7ec4\u4ef6\u6837\u5f0f *\/<\/p>\n<p>}<\/p>\n<p>&lt;\/style&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u7f16\u5199\u7ec4\u4ef6\u6587\u6863<\/h3>\n<\/p>\n<p><p>\u521b\u5efa\u4e00\u4e2a README.md \u6587\u4ef6\uff0c\u5728\u5176\u4e2d\u5199\u660e\u5982\u4f55\u5b89\u88c5\u3001\u5bfc\u5165\u53ca\u4f7f\u7528\u4f60\u7684\u7ec4\u4ef6\u3002<strong>\u597d\u7684\u6587\u6863\u4f1a\u6781\u5927\u5730\u589e\u52a0\u4f60\u7684\u7ec4\u4ef6\u7684\u53ef\u7528\u6027<\/strong>\u3002\u786e\u4fdd\u63d0\u4f9b\u8db3\u591f\u7684\u793a\u4f8b\u548c\u8bf4\u660e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-markdown\"># My Vue3 Component<\/p>\n<p>## Installation<\/p>\n<p>```shell<\/p>\n<p>npm install my-vue3-component<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>Usage<\/h2>\n<\/p>\n<p><pre><code class=\"language-vue\">&lt;template&gt;<\/p>\n<p>  &lt;MyComponent \/&gt;<\/p>\n<p>&lt;\/template&gt;<\/p>\n<p>&lt;script&gt;<\/p>\n<p>import MyComponent from &#039;my-vue3-component&#039;;<\/p>\n<p>export default {<\/p>\n<p>  components: {<\/p>\n<p>    MyComponent<\/p>\n<p>  }<\/p>\n<p>}<\/p>\n<p>&lt;\/script&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u914d\u7f6e\u5305\u4fe1\u606f<\/h3>\n<\/p>\n<p><p>\u5728\u9879\u76ee\u6839\u76ee\u5f55\u4e0b\u7684 <code>package.json<\/code> \u6587\u4ef6\u4e2d\uff0c\u914d\u7f6e\u4f60\u7684\u7ec4\u4ef6\u5305\u7684\u4fe1\u606f\uff0c\u5305\u62ec\u540d\u79f0\u3001\u7248\u672c\u3001\u63cf\u8ff0\u7b49\u3002\u540c\u65f6\u522b\u5fd8\u4e86\u6307\u5b9a\u5165\u53e3\u6587\u4ef6\uff08\u901a\u5e38\u662f\u6253\u5305\u540e\u7684\u6587\u4ef6\uff09\uff0c\u548c peerDependencies\uff08\u5982\u679c\u4f60\u7684\u7ec4\u4ef6\u4f9d\u8d56\u4e8e\u5916\u90e8\u7684 Vue \u5b9e\u4f8b\uff09\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-json\">{<\/p>\n<p>  &quot;name&quot;: &quot;my-vue3-component&quot;,<\/p>\n<p>  &quot;version&quot;: &quot;1.0.0&quot;,<\/p>\n<p>  &quot;description&quot;: &quot;A Vue 3 component&quot;,<\/p>\n<p>  &quot;m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n&quot;: &quot;dist\/my-vue3-component.umd.js&quot;,<\/p>\n<p>  &quot;peerDependencies&quot;: {<\/p>\n<p>    &quot;vue&quot;: &quot;^3.0.0&quot;<\/p>\n<p>  }<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u6d4b\u8bd5\u7ec4\u4ef6<\/h3>\n<\/p>\n<p><p>\u5728\u5c06\u7ec4\u4ef6\u53d1\u5e03\u5230 npm \u4e4b\u524d\uff0c\u9700\u8981\u5145\u5206\u6d4b\u8bd5\u5b83\u7684\u529f\u80fd\u6765\u786e\u8ba4\u6ca1\u6709\u95ee\u9898\u3002\u53ef\u901a\u8fc7\u5355\u5143\u6d4b\u8bd5\u548c E2E\uff08\u7aef\u5230\u7aef\uff09\u6d4b\u8bd5\u7b49\u65b9\u6cd5\u6765\u4fdd\u8bc1\u7ec4\u4ef6\u7684\u8d28\u91cf\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u6253\u5305\u7ec4\u4ef6<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528\u6784\u5efa\u5de5\u5177\u5982 Vue CLI\u3001Vite \u6216 Rollup \u7b49\u6765\u6253\u5305\u4f60\u7684\u7ec4\u4ef6\u3002\u5e94\u786e\u4fdd\u6784\u5efa\u7684\u4ea7\u7269\u652f\u6301\u591a\u79cd\u6a21\u5757\u7cfb\u7edf\uff0c\u6bd4\u5982 ES \u6a21\u5757\u3001CommonJS\u3001UMD \u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-shell\">npm run build<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u53d1\u5e03\u5230 npm<\/h3>\n<\/p>\n<p><p>\u5728\u7ec8\u7aef\u4e2d\u767b\u5f55\u5230\u4f60\u7684 npm \u8d26\u6237\uff0c\u7136\u540e\u53d1\u5e03\u4f60\u7684\u7ec4\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-shell\">npm login<\/p>\n<p>npm publish<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u53d1\u5e03\u4e4b\u540e\uff0c<strong>\u786e\u4fdd\u4f60\u7684\u7ec4\u4ef6\u7248\u6743\u7b26\u5408\u5f00\u6e90\u8bb8\u53ef\u534f\u8bae<\/strong>\uff0c\u6bd4\u5982 MIT\u3001Apache \u7b49\uff0c\u8fd9\u6837\u5176\u4ed6\u5f00\u53d1\u8005\u624d\u80fd\u5b89\u5fc3\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><p>\u4ee5\u4e0a\u5c31\u662f Vue3 \u81ea\u5b9a\u4e49\u7ec4\u4ef6\u53d1\u5e03\u5230 npm \u7684\u5e72\u51c0\u7b80\u6d01\u6d41\u7a0b\u3002\u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0c\u5728\u8fd9\u4e2a\u8fc7\u7a0b\u4e2d\uff0c\u8d28\u91cf\u63a7\u5236\u548c\u6587\u6863\u64b0\u5199\u7684\u7ec6\u8282\u81f3\u5173\u91cd\u8981\uff0c\u5b83\u4eec\u662f\u4f60\u7684\u7ec4\u4ef6\u80fd\u5426\u6210\u529f\u548c\u88ab\u5e7f\u6cdb\u91c7\u7528\u7684\u5173\u952e\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>1. \u5982\u4f55\u521b\u5efa\u5e76\u53d1\u5e03\u81ea\u5b9a\u4e49\u7ec4\u4ef6\u7ed9npm\uff1f<\/strong><br \/>\n\u53d1\u5e03\u81ea\u5b9a\u4e49\u7ec4\u4ef6\u5230npm\u9700\u8981\u4ee5\u4e0b\u51e0\u4e2a\u6b65\u9aa4\uff1a<\/p>\n<ul>\n<li>\u9996\u5148\uff0c\u4f7f\u7528Vue CLI\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u9879\u76ee\uff0c\u5e76\u7f16\u5199\u4f60\u7684\u81ea\u5b9a\u4e49\u7ec4\u4ef6\u3002<\/li>\n<li>\u7136\u540e\uff0c\u786e\u4fdd\u5728\u9879\u76ee\u7684<code>package.json<\/code>\u6587\u4ef6\u4e2d\u6dfb\u52a0\u6b63\u786e\u7684\u540d\u79f0\u3001\u7248\u672c\u53f7\u548c\u63cf\u8ff0\u3002<\/li>\n<li>\u63a5\u4e0b\u6765\uff0c\u5728\u547d\u4ee4\u884c\u4e2d\u4f7f\u7528<code>npm login<\/code>\u547d\u4ee4\u767b\u5f55\u5230\u4f60\u7684npm\u8d26\u53f7\u3002<\/li>\n<li>\u7136\u540e\uff0c\u4f7f\u7528<code>npm publish<\/code>\u547d\u4ee4\u5c06\u4f60\u7684\u7ec4\u4ef6\u53d1\u5e03\u5230npm\u4ed3\u5e93\u4e2d\u3002<\/li>\n<\/ul>\n<p><strong>2. \u5982\u4f55\u8bbe\u7f6eVue3\u81ea\u5b9a\u4e49\u7ec4\u4ef6\u7684\u5165\u53e3\u6587\u4ef6\uff1f<\/strong><br \/>\n\u5728Vue3\u4e2d\uff0c\u81ea\u5b9a\u4e49\u7ec4\u4ef6\u7684\u5165\u53e3\u6587\u4ef6\u662f\u4e00\u4e2a<code>.vue<\/code>\u6587\u4ef6\uff0c\u4f60\u9700\u8981\u5728\u7ec4\u4ef6\u7684<code>package.json<\/code>\u6587\u4ef6\u7684<code>main<\/code>\u5b57\u6bb5\u4e2d\u6307\u5b9a\u5165\u53e3\u6587\u4ef6\u7684\u8def\u5f84\u3002<br \/>\n\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u7684\u5165\u53e3\u6587\u4ef6\u662f<code>src\/index.vue<\/code>\uff0c\u5219\u5728<code>package.json<\/code>\u6587\u4ef6\u4e2d\u7684<code>main<\/code>\u5b57\u6bb5\u5e94\u8be5\u662f<code>&quot;main&quot;: &quot;dist\/index.js&quot;<\/code>\u3002<\/p>\n<p><strong>3. \u5982\u4f55\u5728Vue3\u4e2d\u4f7f\u7528\u5df2\u53d1\u5e03\u7684\u81ea\u5b9a\u4e49\u7ec4\u4ef6\uff1f<\/strong><br \/>\n\u8981\u5728Vue3\u9879\u76ee\u4e2d\u4f7f\u7528\u5df2\u53d1\u5e03\u7684\u81ea\u5b9a\u4e49\u7ec4\u4ef6\uff0c\u4f60\u9700\u8981\u5148\u5b89\u88c5\u5b83\u3002\u5728\u4f60\u7684\u9879\u76ee\u6587\u4ef6\u5939\u4e0b\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<pre><code>npm install your-custom-component\n<\/code><\/pre>\n<p>\u7136\u540e\uff0c\u5728\u9700\u8981\u4f7f\u7528\u8be5\u7ec4\u4ef6\u7684\u5730\u65b9\uff0c\u4f7f\u7528<code>import<\/code>\u8bed\u53e5\u5c06\u5176\u5bfc\u5165\uff1a<\/p>\n<pre><code class=\"language-javascript\">import YourCustomComponent from &#039;your-custom-component&#039;;\n<\/code><\/pre>\n<p>\u63a5\u4e0b\u6765\uff0c\u4f60\u5c31\u53ef\u4ee5\u5728\u6a21\u677f\u4e2d\u4f7f\u7528\u8be5\u7ec4\u4ef6\u4e86\uff1a<\/p>\n<pre><code class=\"language-html\">&lt;template&gt;\n  &lt;div&gt;\n    &lt;your-custom-component \/&gt;\n  &lt;\/div&gt;\n&lt;\/template&gt;\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"Vue3 \u81ea\u5b9a\u4e49\u7ec4\u4ef6\u53d1\u5e03\u5230 npm \u7684\u57fa\u672c\u6b65\u9aa4\u5305\u62ec\uff1a\u521b\u5efa\u4e00\u4e2a\u7ec4\u4ef6\u3001\u7f16\u5199\u8be5\u7ec4\u4ef6\u7684\u6e90\u4ee3\u7801\u548c\u6837\u5f0f\u3001\u7f16\u5199\u7ec4\u4ef6\u7684\u4f7f\u7528\u6587\u6863 [&hellip;]","protected":false},"author":3,"featured_media":298576,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/298567"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=298567"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/298567\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/298576"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=298567"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=298567"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=298567"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}