component.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. import Vue from "vue";
  2. import Cool from "cool";
  3. import store from '@/store'
  4. import router from '@/router'
  5. import { deepMerge, isFunction, isObject } from "../utils";
  6. export default function (options = {}) {
  7. if (!options.events) {
  8. options.events = {};
  9. }
  10. // 组件模块
  11. let componentModules = [];
  12. // 组件列表
  13. let components = [];
  14. // 安装组件
  15. function install(comp) {
  16. let { store: _store, components, service, directives, filters, pages, views, name } = comp;
  17. let { onInstall, onSuccess, onFail } = options.events[name] || {};
  18. try {
  19. const next = () => {
  20. // 注册vuex模块
  21. if (_store) {
  22. for (let i in _store) {
  23. store.registerModule(`${name}-${i}`, _store[i]);
  24. }
  25. }
  26. // 注册组件
  27. if (components) {
  28. for (let i in components) {
  29. Vue.component(components[i].name, components[i]);
  30. }
  31. }
  32. // 注册请求服务
  33. if (service) {
  34. deepMerge(store.$service, service);
  35. }
  36. // 注册指令
  37. if (directives) {
  38. for (let i in directives) {
  39. Vue.directive(i, directives[i]);
  40. }
  41. }
  42. // 注册过滤器
  43. if (filters) {
  44. for (let i in filters) {
  45. Vue.filter(i, filters[i]);
  46. }
  47. }
  48. // 注册页面
  49. if (pages) {
  50. pages.forEach((e) => {
  51. router.addRoute(e);
  52. });
  53. }
  54. // 注册视图
  55. if (views) {
  56. views.forEach((e) => {
  57. if (!e.meta) {
  58. e.meta = {};
  59. }
  60. if (e.moduleName) {
  61. componentModules.push(e);
  62. } else {
  63. e.meta.label = e.label;
  64. if (e.path) {
  65. router.$plugin.addViews([e], {
  66. ignore404: true
  67. });
  68. } else {
  69. console.error(`[${name}-views]:path in null`);
  70. }
  71. }
  72. });
  73. }
  74. // 包安装成功
  75. if (onSuccess) onSuccess(comp);
  76. };
  77. // 安装前
  78. if (onInstall) {
  79. onInstall(comp, { next });
  80. } else {
  81. next();
  82. }
  83. } catch (e) {
  84. console.error(e);
  85. // 安装失败
  86. if (onFail) onFail(comp, e);
  87. }
  88. }
  89. // 解析组件
  90. Cool.components.map((e) => {
  91. if (!e) {
  92. return null;
  93. }
  94. let comp = null;
  95. if (isObject(e)) {
  96. comp = e;
  97. } else {
  98. comp = {
  99. name: e[0],
  100. value: e[1],
  101. options: e[2]
  102. };
  103. }
  104. if (comp.value) {
  105. if (isFunction(comp.value.install)) {
  106. comp.value = comp.value.install(Vue, comp.options);
  107. }
  108. }
  109. // 是否开启
  110. if (comp.options && comp.options.enable === false) {
  111. return null;
  112. }
  113. if (comp) {
  114. comp = {
  115. name: comp.name,
  116. options: comp.options,
  117. ...comp.value
  118. };
  119. components.push(comp);
  120. install(comp);
  121. }
  122. });
  123. // 设置缓存
  124. store.commit("SET_COMPONENT_MODULES", componentModules);
  125. store.commit("SET_COMPONENT", components);
  126. }