将vue2组件发布成一个npm包

参照
https://juejin.cn/post/6867798692460494861
v在这里插入图片描述
文件目录如上

1. npm init -y

2. src目录

index.js

import Toast from './toast.vue'

Toast.install = (Vue) => {
  Vue.prototype.$toast = (msg, duration) => {   //添加一个实例方法 这样全局的实例都可以调用$toast方法了 msg、duration是调用$toast时传入的两个参数
    if (!msg) {
      return;
    }
    duration = duration || 1500;  //如果不传toast持续时间则默认使用此时间
    const constroct = Vue.extend(Toast)  //构造器
    const instance = new constroct();   //创建实例
    instance.msg = msg || '';           //将$toast(msg,duration)中的msg传入组件的data中
    const tpl = instance.$mount().$el   //vue实例未挂载时可这样拿到它的dom 后续可对它的dom进行操作

    document.querySelector('body').appendChild(tpl);
    setTimeout(() => {
      document.querySelector('body').removeChild(tpl)
    }, duration);
  }
}

export default Toast;

toast.vue

<template>
  <div class="toast">{{msg}}</div>
</template>
<script>
export default {
  name: "Toast",
  data() {
    return {
      msg: ""
    };
  }
};
</script>
<style scoped>
.toast {
  position: fixed;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);

  width: fit-content;
  height: 44px;
  background-color: rgba(0, 0, 0);
  display: flex;
  justify-content: center;
  align-items: center;
  padding-left: 10px;
  padding-right: 10px;
  color: #fff;
  border-radius: 5px;
}
</style>

pakage.json

{
  "name": "/* toast-component11 */",
  "version": "3.0.9",
  "description": "",
  "main": "dist/toast.js",
  "scripts": {
    "build": "webpack --mode production"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "@babel/core": "^7.11.5",
    "@babel/preset-env": "^7.11.5",
    "babel-loader": "^8.1.0",
    "css-loader": "^4.2.2",
    "style-loader": "^1.2.1",
    "vue-loader": "^15.9.3",
    "vue-template-compiler": "^2.6.12",
    "webpack": "^4.44.1",
    "webpack-cli": "^3.3.12"
  }
}

3.npm i

4. npm run build

5.npm pack(这一步主要是生成toast-component-1.0.0.tgz,为了在本地测试,可用可不用)

6. 发布包

先登录

设置源 npm config set registry https://registr.npmjs.org (如果是公司的源请自行更换)
npm login
npm adduser(这一步如果报错)

npm ERR! Unexpected token < in JSON at position 0 while parsing near ‘<!DOCTYPE HTML P
运行

// 顺序执行如下代码即可
npm config get proxy
npm config get https-proxy
npm config set registry https://registry.npmjs.org

正常之后输入你的npm的账号和密码
npm publish

项目中使用

yarn add toast-component11 
或者  npm install toast-component11  

main.js

import Toast from 'toast-component11'
Vue.use(Toast)

组件中使用

<template>
  <div class="hello">
    <div @click="showToast">点我出toast</div>
  </div>
</template>

<script>
export default {
  name: 'HelloWorld',
  props: {
    msg: String
  },
  data() {
    return {
    }
  },
  methods: {
    showToast() {
      this.$toast('hello world', 2000)
    }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped lang="stylus">
</style>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要将 Vue2 项目打库并发布npm 上,可以按照以下步骤进行操作: 1. 在项目根目录下创建一个名为 `package.json` 的文件,用于描述你的库信息和依赖项。可以使用 `npm init` 命令自动生模板。 2. 安装必要的依赖项,例如 `vue`、`rollup`、`rollup-plugin-babel` 等。其中 `rollup` 是一个 JavaScript 模块打器,用于将 Vue2 项目打一个库;`rollup-plugin-babel` 则是用来将 ES6+ 语法转换 ES5 语法。 ```bash npm install vue rollup rollup-plugin-babel --save-dev ``` 3. 在项目根目录下创建一个名为 `src` 的文件夹,用于存放你的源代码。可以在该目录下创建一个 `index.js` 文件,用于导出你的 Vue2 组件或插件。 4. 在项目根目录下创建一个名为 `rollup.config.js` 的文件,用于配置打参数。具体配置可以参考下面的示例: ```javascript import babel from 'rollup-plugin-babel'; import commonjs from 'rollup-plugin-commonjs'; import resolve from 'rollup-plugin-node-resolve'; import vue from 'rollup-plugin-vue'; export default { input: 'src/index.js', output: { name: 'MyLibrary', file: 'dist/my-library.js', format: 'umd', globals: { vue: 'Vue' } }, plugins: [ vue(), resolve(), commonjs(), babel({ exclude: 'node_modules/**' }) ], external: ['vue'] }; ``` 5. 在 `package.json` 文件中添加 `scripts` 字段,用于定义打命令和发布命令。例如: ```json "scripts": { "build": "rollup -c", "prepublishOnly": "npm run build" } ``` 上面的配置表示,运行 `npm run build` 命令会执行 `rollup -c` 命令进行打;运行 `npm publish` 命令时会先执行 `npm run build` 命令,然后再执行发布操作。 6. 执行 `npm login` 命令登录 npm 账号,如果没有账号可以先注册一个。 7. 执行 `npm publish` 命令将库发布npm 上。如果发布功,其他人就可以通过 `npm install` 命令安装你的库并使用了。 以上就是将 Vue2 项目打库并发布npm 上的基本步骤,具体细节可以根据实际情况进行调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值