|
1 | 1 | import Vue from 'vue'
|
2 | 2 | import Preview from '@/components/base/preview/preview'
|
3 | 3 |
|
| 4 | + |
4 | 5 | const previewImage = {}
|
5 | 6 | previewImage.install = (Vue, options = {}) => { // eslint-disable-line
|
6 | 7 | const PreviewConstructor = Vue.extend(Preview)
|
7 | 8 |
|
| 9 | + let instance = null |
| 10 | + |
8 | 11 | // eslint-disable-next-line func-names
|
9 | 12 | PreviewConstructor.prototype.close = function () {
|
10 | 13 | this.data = []
|
11 | 14 | this.options = {}
|
12 | 15 | this.imageIndex = 0
|
13 | 16 | }
|
14 | 17 |
|
| 18 | + const getInstance = () => { |
| 19 | + if (!instance) { |
| 20 | + instance = new PreviewConstructor() |
| 21 | + } |
| 22 | + return instance |
| 23 | + } |
| 24 | + |
15 | 25 | Vue.prototype.$imagePreview = (opts = {}) => { // eslint-disable-line
|
16 | 26 | const elem = document.createElement('div')
|
17 |
| - let instance = new PreviewConstructor() |
18 |
| - instance.$mount(elem) |
19 |
| - instance.data = opts.images || [] |
20 |
| - instance.imageIndex = opts.index || 0 |
21 |
| - instance.options = opts.defaultOpt || {} |
22 |
| - document.body.appendChild(instance.$el) |
23 |
| - instance.$on('close', () => { |
24 |
| - instance.close() |
25 |
| - document.body.removeChild(instance.$el) |
26 |
| - instance.$destroy() |
27 |
| - instance = null |
28 |
| - }) |
| 27 | + if (!instance) { |
| 28 | + let myInstance = getInstance() |
| 29 | + Vue.prototype.$previewInstance = myInstance // eslint-disable-line |
| 30 | + myInstance.$mount(elem) |
| 31 | + myInstance.data = opts.images || [] |
| 32 | + myInstance.imageIndex = opts.index || 0 |
| 33 | + myInstance.options = opts.defaultOpt || {} |
| 34 | + document.body.appendChild(myInstance.$el) |
| 35 | + myInstance.$on('close', () => { |
| 36 | + myInstance.close() |
| 37 | + document.body.removeChild(myInstance.$el) |
| 38 | + myInstance.$destroy() |
| 39 | + myInstance = null |
| 40 | + instance = null |
| 41 | + }) |
| 42 | + } |
29 | 43 | }
|
30 | 44 | }
|
31 | 45 |
|
|
0 commit comments