@tmsfe/tms-core 0.0.214 → 0.0.215
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json
CHANGED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @desc: 异步组件Behavior
|
|
3
|
+
* 父组件和子组件中都需要注册
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
const getPromiseName = id => `__comLoadPromise_${id}`;
|
|
7
|
+
const getResolveName = id => `__comLoadPromiseResolve_${id}`;
|
|
8
|
+
|
|
9
|
+
const asyncSelectBehavior = Behavior({
|
|
10
|
+
lifetimes: {
|
|
11
|
+
attached() {
|
|
12
|
+
if (!this.id) {
|
|
13
|
+
return;
|
|
14
|
+
}
|
|
15
|
+
const owner = this.selectOwnerComponent();
|
|
16
|
+
// 组件加载完毕向父组件标记自己加载完毕
|
|
17
|
+
if (owner) {
|
|
18
|
+
const id = `#${this.id}`;
|
|
19
|
+
const promiseName = getPromiseName(id);
|
|
20
|
+
const resolveName = getResolveName(id);
|
|
21
|
+
// 父组件调用asyncSelectComponent时才创建promise桥梁,这里可能创建了,可能没有
|
|
22
|
+
if (owner[resolveName] === undefined) {
|
|
23
|
+
owner[promiseName] = Promise.resolve(this);
|
|
24
|
+
} else {
|
|
25
|
+
owner[resolveName](this);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
},
|
|
29
|
+
},
|
|
30
|
+
methods: {
|
|
31
|
+
// 异步组件selectComponent函数,父组件调用
|
|
32
|
+
asyncSelectComponent(id, opts = {}) {
|
|
33
|
+
const { cache = true } = opts;
|
|
34
|
+
const promiseName = getPromiseName(id);
|
|
35
|
+
if (cache && this[promiseName] !== undefined) {
|
|
36
|
+
return this[promiseName];
|
|
37
|
+
}
|
|
38
|
+
this[promiseName] = new Promise((resolve) => {
|
|
39
|
+
this[getResolveName(id)] = resolve;
|
|
40
|
+
});
|
|
41
|
+
return this[promiseName];
|
|
42
|
+
},
|
|
43
|
+
},
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
module.exports = asyncSelectBehavior;
|