npm发布组件(vue3+webpack)

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

javascript 复制代码
// 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;
javascript 复制代码
<!-- 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>
javascript 复制代码
<!-- 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.本地测试

javascript 复制代码
<!-- 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.打包

javascript 复制代码
 "package": "vue-cli-service build --target lib ./src/package/index.js --name wlz-component-ui --dest wlz-component-ui"
javascript 复制代码
npm run package

6.发布(注意!!要进入新生成的目录操作即:wlz-component-ui)

javascript 复制代码
cd .\wlz-component-ui\
javascript 复制代码
npm init -y
javascript 复制代码
{
  "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组件库"
}
javascript 复制代码
npm adduser

按回车登录

发布

javascript 复制代码
npm publish

7使用

javascript 复制代码
npm i wlz-component-ui
javascript 复制代码
// 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");
javascript 复制代码
<!-- 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>
相关推荐
前端混子14 分钟前
我用 AI 做了个「禾月小历」,一套代码跑微信小程序和 H5
前端
IT_陈寒43 分钟前
Java实战技巧总结:当你的finally块没有执行时会发生什么?
前端·人工智能·后端
小帅不太帅1 小时前
一年一次黑五,为了买软件真省钱,我做了个开源小站
前端·人工智能·github
吴名氏.2 小时前
从零手写一个 AI Agent 框架(TypeScript 完整实现):Function Calling 循环、流式输出与错误恢复
人工智能·typescript·node.js·大模型·ai agent·functioncalling·llm 应用开发
JavaGuide3 小时前
刚刚,阿里版 Codex 上线了匿名模型 HungerG,无限 Token!
前端·后端
zhanlang02173 小时前
HTML 零基础入门到企业实战全套教程【第九章 9.3】移动端 HTML 基础适配入门
前端·html
用户69371750013843 小时前
AI 帮我写了大半年的代码,我却越来越不会写代码了
前端·后端·github
skywalk81633 小时前
把「假成功」赶出工具层:lightharness 两天升级复盘(4×5 模式矩阵 + 工具层 5 个缺陷)
开发语言·前端·人工智能·lightharness
彭亚川Allen4 小时前
从零手搓 RAG:用 Elasticsearch 存储与检索向量
前端·后端·面试
jerrywus4 小时前
15 个可以直接安装的 AI Agent Skills:按场景挑
前端·agent·claude