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)文章
Vue腳手架配置代理服務(wù)器的兩種方式(小結(jié))
本文主要介紹了Vue腳手架配置代理服務(wù)器的兩種方式(小結(jié)),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-01-01使用live-server快速搭建本地服務(wù)器+自動刷新的方法
下面小編就為大家分享一篇使用live-server快速搭建本地服務(wù)器+自動刷新的方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-03-03Vue3+Vite+ElementPlus管理系統(tǒng)常見問題
本文記錄了使用Vue3+Vite+ElementPlus從0開始搭建一個前端工程會面臨的常見問題,沒有技術(shù)深度,但全都是解決實(shí)際問題的干貨,可以當(dāng)作是問題手冊以備后用,感興趣的朋友參考下2023-12-12elementui中tabel組件的scope.$index的使用及說明
這篇文章主要介紹了elementui中tabel組件的scope.$index的使用及說明,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-10-10vue.js中引入vuex儲存接口數(shù)據(jù)及調(diào)用的詳細(xì)流程
這篇文章主要給大家介紹了關(guān)于在vue.js中引入vuex儲存接口數(shù)據(jù)及調(diào)用的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-12-12Vuex拿到state中數(shù)據(jù)的3種方式與實(shí)例剖析
store是一個狀態(tài)管理工具(vueX中只有唯一 一個store),下面這篇文章主要給大家介紹了關(guān)于Vuex拿到state中數(shù)據(jù)的3種方式與實(shí)例剖析的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-09-09