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>
相关推荐
软件派19 小时前
生产动态数据智能风控:阈值判别、趋势预测、异常识别算法工程落地实战
前端·数据库·算法
花开盛夏^.^1 天前
电脑打开多个浏览器Web窗口占用较大的运行内存的原因
前端·浏览器
馒头不想说话1 天前
实体店老板的24小时AI运营员工,写、剪、发全自动,省下一整个团队工资
前端
椒哥1 天前
一天被问几十次"允许",我统计了 517 次,然后写了个插件
前端·javascript·github
风骏时光牛马1 天前
生成一个AI稳定性治理及疑难问题分析标题,只需要一个标题,不需要其他的任何文本。
前端
莫西干式秃头1 天前
用 AI + threejs 写了个浏览器里的 3D 动作游戏《武来》:砍怪、放技能、开 SU7
前端·人工智能·three.js
kyriewen1 天前
我测了 6 种防请求竞态的写法:只有 1 种真能打
前端·javascript·程序员
孟健1 天前
用 WorkBuddy 做个人名片网页,挂上开源设计技能后效果完全不同了
前端·人工智能·ai编程
IT_陈寒1 天前
SpringBoot自动配置的坑,这次终于爬出来了
前端·人工智能·后端
LuHang1 天前
从天地图切换到高德地图:一次真实的底图迁移实战
前端·vue.js·cesium