-
Notifications
You must be signed in to change notification settings - Fork 125
/
my-single-spa-vue.js
95 lines (74 loc) · 2.46 KB
/
my-single-spa-vue.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/**
* @file
* Created by zhangyatao on 2019/10/21.
*/
'use strict';
(function (global) {
var PREFIX_ID = '#my-single-spa_';
function checkElement(boxId) {
var ele = boxId + ' #mss_container';
var div = null;
if (!document.querySelector(boxId)) {
div = document.createElement('div');
div.id = boxId.slice(1);
document.body.appendChild(div);
}
if (!document.querySelector(ele)) {
div = document.createElement('div');
div.id = 'mss_container';
document.querySelector(boxId).appendChild(div);
}
return ele;
}
function emptyElement(boxId) {
var box = document.querySelector(boxId);
if (box) {
box.innerHTML = '';
}
}
var defaultOpts = {
Vue: null,
appOptions: null,
template: null,
};
function bootstrap(opts) {
return Promise.resolve();
}
function mount(opts, mountedInstances, props) {
var el = opts.appOptions.el;
var boxId = PREFIX_ID + props.name;
if (!el || String(el).indexOf(boxId) === 0) {
opts.appOptions.el = checkElement(boxId);
}
mountedInstances.instance = new opts.Vue(opts.appOptions);
if (mountedInstances.instance.bind) {
mountedInstances.instance = mountedInstances.instance.bind(mountedInstances.instance);
}
return Promise.resolve();
}
function unmount(opts, mountedInstances, props) {
mountedInstances.instance.$destroy();
mountedInstances.instance.$el.innerHTML = '';
emptyElement(PREFIX_ID + props.name);
delete mountedInstances.instance;
return Promise.resolve();
}
global.mySingleSpaVue = function (options) {
if (typeof options !== 'object') {
throw new Error('mySingleSpaVue options must be a object');
}
let opts = Object.assign({}, defaultOpts, options);
if (!options.Vue) {
throw new Error('mySingleSpaVue must be passed a Vue');
}
if (!options.appOptions) {
throw new Error('mySingleSpaVue must be passed a appOptions');
}
let mountedInstances = {};
return {
bootstrap: bootstrap.bind(null, opts, mountedInstances),
mount: mount.bind(null, opts, mountedInstances),
unmount: unmount.bind(null, opts, mountedInstances)
}
};
}(window));