检查vue的版本

如果使用的是vue-cli3,需要先行安装@vue/cli-init以便使用vue-cli 2.x版本语法:

  1. npm install -g @vue/cli-init //全局安装@vue/cli-init

开始创建vue的开发模板

  1. vue init webpack-simple public-element-prompt-component //创建webpack-simple模板项目

1638544656.png

创建完成后进入项目目录,安装相关依赖 (cnpm install),并运行(npm run dev)测试项目是否成功运行。

clipboard.png

在src目录下新建文件夹(名字随意),并新建一个.vue文件,图示如下:

clipboard.png

文件代码:

  1. <template>
  2. <div class="PublicComponent"></div>
  3. </template>
  4. <script>
  5. export default {
  6. name: 'PublicComponent',
  7. data() {
  8. return {
  9. }
  10. },
  11. props:{
  12. msgConfig:Object,
  13. confirmConfig:Object,
  14. promptConfig:Object
  15. },
  16. watch: {
  17. msgConfig: function (val, oldVal) {
  18. if (val != oldVal) {
  19. this.showMassage(val);
  20. }
  21. },
  22. confirmConfig: function (val, oldVal) {
  23. if (val != oldVal) {
  24. this.showConfirmModal(val);
  25. }
  26. },
  27. promptConfig: function (val, oldVal) {
  28. if (val != oldVal) {
  29. this.showPromptModal(val);
  30. }
  31. }
  32. },
  33. methods: {
  34. showMassage(config) {
  35. this.$message(config);
  36. },
  37. showConfirmModal(config) {
  38. this.$confirm(config.message, config.title, config.options ? config.options : {}).then(config.yesCallback).catch(config.cancelCallback ? config.cancelCallback : () => { });
  39. },
  40. showPromptModal(config) {
  41. this.$prompt(config.message, config.title, config.options ? config.options : {}).then(config.yesCallback).catch(config.cancelCallback ? config.cancelCallback : () => { });
  42. }
  43. }
  44. }
  45. </script>

在组件平级目录下新增index.js文件

  1. import PublicComponent from './PublicComponent.vue'
  2. // Declare install function executed by Vue.use()
  3. export function install(Vue) {
  4. if (install.installed) return;
  5. install.installed = true;
  6. Vue.component('PublicComponent', PublicComponent);
  7. }
  8. // Create module definition for Vue.use()
  9. const plugin = {
  10. install,
  11. };
  12. // Auto-install when vue is found (eg. in browser via <script> tag)
  13. let GlobalVue = null;
  14. if (typeof window !== 'undefined') {
  15. GlobalVue = window.Vue;
  16. } else if (typeof global !== 'undefined') {
  17. GlobalVue = global.Vue;
  18. }
  19. if (GlobalVue) {
  20. GlobalVue.use(plugin);
  21. }
  22. export default PublicComponent;

修改package.json等配置文件

clipboard.png

webpack.config.js文件配置,主要修改入口(entry)及出口信息(output):

  1. ···
  2. module.exports = {
  3. // entry: './src/main.js',
  4. entry: './src/component/index.js',
  5. output: {
  6. path: path.resolve(__dirname, './dist'),
  7. publicPath: '/dist/',
  8. // filename: 'build.js'
  9. filename: 'public-component.js',//与package.json中main相对应
  10. library: 'Public-component',
  11. libraryTarget: 'umd',
  12. umdNamedDefine: true
  13. }
  14. ...
  15. }

以上步骤完成后,我们可以进行打包测试了:

  1. npm run build

clipboard.png

注册npm账号

https://www.npmjs.com/?track=newUserCreated
姓名:npmusernameguan
密码:123456guanxindi

注册成功后:

  1. npm login //登录npm,需要正确填写用户名、密码及邮箱
  2. npm publish //发布到npm

1638544923.png

发布成功后,你的注册邮箱会受到来自npm的邮件,提示你的npm包已经成功发布。这时候我们可以去npm与cnpm搜索上传的包:

npm地址:传送门
cnpm地址:传送门

使用该组件

  1. cnpm install public-element-prompt-component --save-dev //安装发布的npm包

mian.js:

  1. import Vue from 'vue'
  2. import App from './App.vue'
  3. import { Button, Message, MessageBox } from 'element-ui';
  4. Vue.use(Button);
  5. Vue.prototype.$message = Message;
  6. Vue.prototype.$confirm = MessageBox.confirm;
  7. Vue.prototype.$prompt = MessageBox.prompt;
  8. Vue.config.productionTip = false
  9. new Vue({
  10. render: h => h(App),
  11. }).$mount('#app')

App.vue代码如下:

  1. <template>
  2. <div class="hello">
  3. <el-button @click="showComponent" type="danger" plain>删除</el-button>
  4. <publicComponent :msgConfig="publicComponentObj.msgConfig" :confirmConfig="publicComponentObj.confirmConfig"></publicComponent>
  5. </div>
  6. </template>
  7. <script>
  8. import PublicComponent from 'public-element-prompt-component'
  9. export default {
  10. name: 'HelloWorld',
  11. props: {
  12. msg: String
  13. },
  14. data() {
  15. return {
  16. publicComponentObj: {
  17. confirmConfig: {},
  18. msgConfig: {},
  19. }
  20. }
  21. },
  22. methods: {
  23. showComponent() {
  24. const tempObj = {
  25. message: `确认移出选中成员吗?`,
  26. title: '提示',
  27. options: {
  28. type: 'warning'
  29. },
  30. yesCallback: () => {
  31. const tempMsgObj = {
  32. message: '删除成功',
  33. type: 'success',
  34. showClose: true,
  35. duration: 3000
  36. }
  37. this.publicComponentObj.msgConfig = tempMsgObj;
  38. }
  39. }
  40. this.publicComponentObj.confirmConfig = tempObj;
  41. }
  42. },
  43. components: {
  44. PublicComponent
  45. }
  46. }
  47. </script>
  48. <!-- Add "scoped" attribute to limit CSS to this component only -->
  49. <style scoped>
  50. </style>