How to publish plugins to npm

First you need to register an npm account    npm URL : https://www.npmjs.com/

 

 Click Sign in to jump to the login page

 Click Create Account to create a new account

 After the registration is complete, there will be an email to send a one-time password, just verify it at that time.

 

After login, click your avatar and click Account to verify

I have verified it here, so I won’t make any more descriptions.

After the account here is done, put your components into the configured folder. This is how I configured it here.

 

The pluhins here are my components,

README is the documentation

index.js configuration export 

import vueTouch from './lib/scrolls.vue' // 这个就是你平时写的 SFC 组件

// 这里要导出一个包含 install 方法的对象
const plugin = { // 这里要导出一个 install 方法
  install (Vue, options) {
    // 这里写你的代码,你可以全局注册组件,也可以写全局指令,也可以扩展 Vue 的方法
    // 1. 全局组件
    Vue.component(vueTouch.name, vueTouch)
    // 2. 全局方法或属性
    Vue.myGlobalMethod = function () {
      // 逻辑...
    }
    // 3. 全局指令
    Vue.directive('my-directive', {
      bind (el, binding, vnode, oldVnode) {
        // 逻辑...
      }
    })
    // 4. 注入组件
    Vue.mixin({
      created: function () {
        // 逻辑...
      }
    })
    // 5. 添加实例方法
    Vue.prototype.$myMethod = function (methodOptions) {
      // 逻辑...
    }
  }
}
export default plugin

package.json configuration version, name, author, etc. Pay attention to the uniqueness of the name, and change the version number when it is updated

{
  "name": "vue-tree-transfres",
  "version": "1.0.1",
  "description": "vue plugins",
  "main": "index.js",
  "directories": {
    "lib": "lib"
  },
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "J_C",
  "license": "ISC",
  "dependencies": {
    "vue-tree-transfres": "^1.0.1"
  }
}

After the configuration here is complete, open your terminal

Enter npm login for npm account login

 Here, you must check whether your Taobao mirror has been switched to npm, if it is still Taobao mirror execution

npm config set registry https://registry.npmjs.org/

Then enter npm publish to upload the code

it's ok

Guess you like

Origin blog.csdn.net/css_javascrpit/article/details/131520362