vue3+webpack中npm发布组件的实现

 更新时间:2025年01月17日 09:26:39   作者:itwlz  
本文主要介绍了vue3+webpack中npm发布组件的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

1.初始化Vue项目

vue create my-app

2.本地运行

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.发布(注意!!要进入新生成的目录操作即: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

 按回车登录

发布 

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>

到此这篇关于vue3+webpack中npm发布组件的实现的文章就介绍到这了,更多相关vue3 webpack npm发布组件内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Vue脚手架配置代理服务器的两种方式(小结)

    Vue脚手架配置代理服务器的两种方式(小结)

    本文主要介绍了Vue脚手架配置代理服务器的两种方式(小结),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-01-01
  • vue el-radio单选传值和默认选中方式

    vue el-radio单选传值和默认选中方式

    文章介绍了一个父组件和子组件的交互过程,父组件通过点击“关联公司”输入框弹出子组件dialog,子组件中使用SelectCompany.vue实现默认选中功能,作者分享了个人经验,希望能对大家有所帮助
    2025-01-01
  • 使用live-server快速搭建本地服务器+自动刷新的方法

    使用live-server快速搭建本地服务器+自动刷新的方法

    下面小编就为大家分享一篇使用live-server快速搭建本地服务器+自动刷新的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-03-03
  • Vue3+Vite+ElementPlus管理系统常见问题

    Vue3+Vite+ElementPlus管理系统常见问题

    本文记录了使用Vue3+Vite+ElementPlus从0开始搭建一个前端工程会面临的常见问题,没有技术深度,但全都是解决实际问题的干货,可以当作是问题手册以备后用,感兴趣的朋友参考下
    2023-12-12
  • elementui中tabel组件的scope.$index的使用及说明

    elementui中tabel组件的scope.$index的使用及说明

    这篇文章主要介绍了elementui中tabel组件的scope.$index的使用及说明,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-10-10
  • vue中的router-view父子组件传参方式

    vue中的router-view父子组件传参方式

    这篇文章主要介绍了vue中的router-view父子组件传参方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-01-01
  • Vue3中插槽(slot)用法汇总(推荐)

    Vue3中插槽(slot)用法汇总(推荐)

    这篇文章主要介绍了Vue3中插槽(slot)用法汇总,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-08-08
  • vue.js中引入vuex储存接口数据及调用的详细流程

    vue.js中引入vuex储存接口数据及调用的详细流程

    这篇文章主要给大家介绍了关于在vue.js中引入vuex储存接口数据及调用的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧。
    2017-12-12
  • Vuex拿到state中数据的3种方式与实例剖析

    Vuex拿到state中数据的3种方式与实例剖析

    store是一个状态管理工具(vueX中只有唯一 一个store),下面这篇文章主要给大家介绍了关于Vuex拿到state中数据的3种方式与实例剖析的相关资料,文中通过实例代码介绍的非常详细,需要的朋友可以参考下
    2022-09-09
  • Vue非父子组件通信详解

    Vue非父子组件通信详解

    这篇文章主要为大家详细介绍了Vue非父子组件通信的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-06-06

最新评论