You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

119 lines
3.4 KiB

4 months ago
  1. var VueDemi = (function (VueDemi, Vue, VueCompositionAPI) {
  2. if (VueDemi.install) {
  3. return VueDemi
  4. }
  5. if (!Vue) {
  6. console.error('[vue-demi] no Vue instance found, please be sure to import `vue` before `vue-demi`.')
  7. return VueDemi
  8. }
  9. // Vue 2.7
  10. if (Vue.version.slice(0, 4) === '2.7.') {
  11. for (var key in Vue) {
  12. VueDemi[key] = Vue[key]
  13. }
  14. VueDemi.isVue2 = true
  15. VueDemi.isVue3 = false
  16. VueDemi.install = function () {}
  17. VueDemi.Vue = Vue
  18. VueDemi.Vue2 = Vue
  19. VueDemi.version = Vue.version
  20. VueDemi.warn = Vue.util.warn
  21. VueDemi.hasInjectionContext = function() {
  22. return !!VueDemi.getCurrentInstance()
  23. }
  24. function createApp(rootComponent, rootProps) {
  25. var vm
  26. var provide = {}
  27. var app = {
  28. config: Vue.config,
  29. use: Vue.use.bind(Vue),
  30. mixin: Vue.mixin.bind(Vue),
  31. component: Vue.component.bind(Vue),
  32. provide: function (key, value) {
  33. provide[key] = value
  34. return this
  35. },
  36. directive: function (name, dir) {
  37. if (dir) {
  38. Vue.directive(name, dir)
  39. return app
  40. } else {
  41. return Vue.directive(name)
  42. }
  43. },
  44. mount: function (el, hydrating) {
  45. if (!vm) {
  46. vm = new Vue(Object.assign({ propsData: rootProps }, rootComponent, { provide: Object.assign(provide, rootComponent.provide) }))
  47. vm.$mount(el, hydrating)
  48. return vm
  49. } else {
  50. return vm
  51. }
  52. },
  53. unmount: function () {
  54. if (vm) {
  55. vm.$destroy()
  56. vm = undefined
  57. }
  58. },
  59. }
  60. return app
  61. }
  62. VueDemi.createApp = createApp
  63. }
  64. // Vue 2.6.x
  65. else if (Vue.version.slice(0, 2) === '2.') {
  66. if (VueCompositionAPI) {
  67. for (var key in VueCompositionAPI) {
  68. VueDemi[key] = VueCompositionAPI[key]
  69. }
  70. VueDemi.isVue2 = true
  71. VueDemi.isVue3 = false
  72. VueDemi.install = function () {}
  73. VueDemi.Vue = Vue
  74. VueDemi.Vue2 = Vue
  75. VueDemi.version = Vue.version
  76. VueDemi.hasInjectionContext = function() {
  77. return !!VueDemi.getCurrentInstance()
  78. }
  79. } else {
  80. console.error('[vue-demi] no VueCompositionAPI instance found, please be sure to import `@vue/composition-api` before `vue-demi`.')
  81. }
  82. }
  83. // Vue 3
  84. else if (Vue.version.slice(0, 2) === '3.') {
  85. for (var key in Vue) {
  86. VueDemi[key] = Vue[key]
  87. }
  88. VueDemi.isVue2 = false
  89. VueDemi.isVue3 = true
  90. VueDemi.install = function () {}
  91. VueDemi.Vue = Vue
  92. VueDemi.Vue2 = undefined
  93. VueDemi.version = Vue.version
  94. VueDemi.set = function (target, key, val) {
  95. if (Array.isArray(target)) {
  96. target.length = Math.max(target.length, key)
  97. target.splice(key, 1, val)
  98. return val
  99. }
  100. target[key] = val
  101. return val
  102. }
  103. VueDemi.del = function (target, key) {
  104. if (Array.isArray(target)) {
  105. target.splice(key, 1)
  106. return
  107. }
  108. delete target[key]
  109. }
  110. } else {
  111. console.error('[vue-demi] Vue version ' + Vue.version + ' is unsupported.')
  112. }
  113. return VueDemi
  114. })(
  115. ((globalThis || self).VueDemi = (globalThis || self).VueDemi || (typeof VueDemi !== 'undefined' ? VueDemi : {})),
  116. (globalThis || self).Vue || (typeof Vue !== 'undefined' ? Vue : undefined),
  117. (globalThis || self).VueCompositionAPI || (typeof VueCompositionAPI !== 'undefined' ? VueCompositionAPI : undefined)
  118. );