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.本地測(cè)試
<!-- 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組件庫(kù)"
}
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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Vue 組件封裝 并使用 NPM 發(fā)布的教程
- Vue cli3 庫(kù)模式搭建組件庫(kù)并發(fā)布到 npm的流程
- 詳解如何制作并發(fā)布一個(gè)vue的組件的npm包
- Vue.js構(gòu)建你的第一個(gè)包并在NPM上發(fā)布的方法步驟
- 自定義Vue組件打包、發(fā)布到npm及使用教程
- 一個(gè)vue組件庫(kù)發(fā)布到npm的完整實(shí)現(xiàn)過(guò)程
- vue3+vite自定義封裝vue組件發(fā)布到npm包的全過(guò)程
- vue組件打包并發(fā)布到npm的全過(guò)程
- vue使用npm發(fā)布自己的公網(wǎng)包
- vue組件發(fā)布成npm包
相關(guān)文章
Vue腳手架配置代理服務(wù)器的兩種方式(小結(jié))
本文主要介紹了Vue腳手架配置代理服務(wù)器的兩種方式(小結(jié)),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-01-01
使用live-server快速搭建本地服務(wù)器+自動(dòng)刷新的方法
下面小編就為大家分享一篇使用live-server快速搭建本地服務(wù)器+自動(dòng)刷新的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-03-03
Vue3+Vite+ElementPlus管理系統(tǒng)常見(jiàn)問(wèn)題
本文記錄了使用Vue3+Vite+ElementPlus從0開(kāi)始搭建一個(gè)前端工程會(huì)面臨的常見(jiàn)問(wèn)題,沒(méi)有技術(shù)深度,但全都是解決實(shí)際問(wèn)題的干貨,可以當(dāng)作是問(wèn)題手冊(cè)以備后用,感興趣的朋友參考下2023-12-12
elementui中tabel組件的scope.$index的使用及說(shuō)明
這篇文章主要介紹了elementui中tabel組件的scope.$index的使用及說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-10-10
vue.js中引入vuex儲(chǔ)存接口數(shù)據(jù)及調(diào)用的詳細(xì)流程
這篇文章主要給大家介紹了關(guān)于在vue.js中引入vuex儲(chǔ)存接口數(shù)據(jù)及調(diào)用的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2017-12-12
Vuex拿到state中數(shù)據(jù)的3種方式與實(shí)例剖析
store是一個(gè)狀態(tài)管理工具(vueX中只有唯一 一個(gè)store),下面這篇文章主要給大家介紹了關(guān)于Vuex拿到state中數(shù)據(jù)的3種方式與實(shí)例剖析的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-09-09

