在Vue 2中从0开始打包并发布一个NPM包需要以下步骤:
- 创建项目结构和基础代码。
- 编写
package.json
配置文件。 - 编写组件代码。
- 编写
README.md
和LICENSE
文件。 - 确保代码质量和测试。
- 发布到NPM。
以下是一个简化的示例:
my-vue-component/
│
├── src/
│ └── MyComponent.vue
│
├── package.json
├── README.md
└── LICENSE
src/MyComponent.vue
:
<template>
<div class="my-component">Hello, Vue Component!</div>
</template>
<script>
export default {
name: 'MyComponent',
// 组件的其他选项...
}
</script>
<style scoped>
.my-component {
/* 样式 */
}
</style>
package.json
:
{
"name": "my-vue-component",
"version": "1.0.0",
"description": "A simple Vue 2 component",
"main": "dist/my-vue-component.common.js",
"scripts": {
"build": "vue-cli-service build --target lib --name my-vue-component src/MyComponent.vue"
},
"keywords": ["vue", "component"],
"author": "Your Name",
"license": "MIT",
"private": false,
"files": [
"dist",
"src"
],
"peerDependencies": {
"vue": "^2.0.0"
}
}
README.md
:
# My Vue Component
A simple Vue 2 component for doing xyz.
## Installation
```bash
npm install my-vue-component
Usage
<template>
<my-component></my-component>
</template>
<script>
import MyComponent from 'my-vue-component';
export default {
components: {
MyComponent
}
}
</script>
`LICENSE`:
MIT License
Copyright (c) [year] [your name]
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO