Writing in the front

This is my study today, with the written record, left for information inquiry and to meet this problem of a friend reference.

1. Create a Vue plug-in

So let’s write a test demo

<! --index.ts-->export default {
  install(Vue: any, options: object = {}) {
    const log = (): void => {
      console.log('first plugin');
    }
    const add = (a: number, b: number): number => {
      return a * b
    }
    Vue.prototype.$log = log
    Vue.prototype.$add = add
  }
}
Copy the code

2. Create an NPM package

Use the command line to open the directory where index.ts resides, NPM init, and press Enter to create package.json

3. Register an NPM account

NPM registered address

4. Switch to the NPM source

Enter NPM config set registry http://www.npmjs.org

5. Login NPM

On the cli, enter NPM login

Enter your Username, Password, and Email in sequence

The console output is Logged in as < Username > on https://registry.npmjs.org//. Log in successfully

6. Release plug-ins

Enter NPM publish –access=public on the command line

In case of an error

 try renaming your package to '@username/xxx' and publishing with 'npm publish --access=public' instead
Copy the code

Enter package.json to modify the name field (you can search for the name of the package on NPM’s official website, if you can’t find it, our package is available)

NPM publish –access=public

+ @ XXX/[email protected]Copy the code

This output description is complete, our plug-in has been published to NPM, go to NPM install XXX to try it.