vue3+webpack中npm發(fā)布組件的實(shí)現(xiàn)
1.初始化Vue項(xiàng)目
vue create my-app
2.本地運(yùn)行
npm run serve

3.新增目錄和文件
1. src/package/index.js
2. src/package/wlz-btn/index.vue
3. src/package/wlz-input/index.vue

// src\package\index.js
import WlzBtn from "./wlz-btn";
import WlzInput from "./wlz-input";
const componentList = [WlzBtn, WlzInput];
const install = function (Vue) {
componentList.forEach((com) => {
Vue.component(com.name, com);
});
};
export default install;
<!-- src\package\wlz-btn\index.vue -->
<template>
<button class="wlz-btn">你好呀</button>
</template>
<script>
export default {
name: "WlzBtn",
};
</script>
<style scoped>
.wlz-btn {
background-color: #4caf50;
color: white;
padding: 15px 32px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
margin: 4px 2px;
cursor: pointer;
}
</style>
<!-- src\package\wlz-input\index.vue -->
<template>
<input type="text" class="wlz-input" />
</template>
<script>
export default {
name: "WlzInput",
};
</script>
<style scoped>
.wlz-input {
padding: 10px;
border: 1px solid red;
border-radius: 4px;
box-sizing: border-box;
}
</style>
4.本地測試
<!-- src\App.vue -->
<template>
<div>
<p>111</p>
<WlzBtn />
<WlzInput />
<p>222</p>
</div>
</template>
<script>
import WlzBtn from "@/package/wlz-btn";
import WlzInput from "@/package/wlz-input";
export default {
name: "App",
components: {
WlzBtn,
WlzInput,
},
};
</script>
<style>
#app {
font-family: Avenir, Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: center;
color: #2c3e50;
margin-top: 60px;
}
</style>

5.打包
"package": "vue-cli-service build --target lib ./src/package/index.js --name wlz-component-ui --dest wlz-component-ui"

npm run package

6.發(fā)布(注意??!要進(jìn)入新生成的目錄操作即:wlz-component-ui)
cd .\wlz-component-ui\
npm init -y
{
"name": "wlz-ui",
"version": "1.0.0",
"main": "wlz-ui.common.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"description": "我的ui組件庫"
}
npm adduser
按回車登錄

發(fā)布
npm publish


7使用
npm i wlz-component-ui
// src\main.js
import { createApp } from "vue";
import App from "./App.vue";
import WlzComponentUI from "wlz-component-ui";
import "wlz-component-ui/wlz-component-ui.css";
const app = createApp(App);
app.use(WlzComponentUI);
app.mount("#app");
<!-- src\App.vue -->
<template>
<div>
<p>1111</p>
<WlzBtn />
<WlzInput />
<p>222</p>
</div>
</template>
<script>
export default {
name: "App",
};
</script>
<style>
#app {
font-family: Avenir, Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: center;
color: #2c3e50;
margin-top: 60px;
}
</style>

到此這篇關(guān)于vue3+webpack中npm發(fā)布組件的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)vue3 webpack npm發(fā)布組件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue3 Element Plus el-tabs數(shù)據(jù)刷新實(shí)現(xiàn)方式
這篇文章主要介紹了Vue3 Element Plus el-tabs數(shù)據(jù)刷新實(shí)現(xiàn)方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2025-07-07
vue項(xiàng)目中使用mapbox地圖切換底圖的詳細(xì)教程
最近開始入坑前端mapbox地圖,跟大家一起慢慢深入學(xué)習(xí),下面這篇文章主要給大家介紹了關(guān)于vue項(xiàng)目中使用mapbox地圖切換底圖的詳細(xì)教程,文中給出了詳細(xì)的實(shí)例代碼,需要的朋友可以參考下2023-04-04
vue常用的數(shù)字孿生可視化的自適應(yīng)方案
這篇文章主要為大家介紹了vue常用的數(shù)字孿生可視化的自適應(yīng)方案詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07
如何在Vue和Spring?boot之間實(shí)現(xiàn)前后端連接
最近搭建一個Springboot+vue前后端分離項(xiàng)目,真的很簡單,下面這篇文章主要給大家介紹了關(guān)于如何在Vue和Spring?boot之間實(shí)現(xiàn)前后端連接的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-05-05
Vue手把手教你擼一個 beforeEnter 鉤子函數(shù)
這篇文章主要介紹了Vue手把手教你擼一個 beforeEnter 鉤子函數(shù),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-04-04

