-
Notifications
You must be signed in to change notification settings - Fork 0
/
import-vue2.vue
53 lines (46 loc) · 1.22 KB
/
import-vue2.vue
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
<script>
import { cloneDeep } from 'lodash-es';
// xxx => 指代vue2 npm组件包。
import Component from 'xxx';
const CustomComponent = cloneDeep(Component);
const lifeCycleChangeObj = {
beforeDestroy: 'beforeUnmount',
destroyed: 'unmounted',
}
for(let [oKey, nKey] of Object.entries(lifeCycleChangeObj)) {
if(CustomComponent[oKey]) {
CustomComponent[nKey] = CustomComponent[oKey];
Reflect.deleteProperty(CustomComponent, oKey);
}
}
export default {
name: 'CustomComponent',
// vue2 组件内部定义的$emit(xxx) xxx要写到这里,不然会出现警告。如内部定义了 this.$emit('data', {}), 则 emits: ['data'];
emits: [],
...CustomComponent,
};
// or
export default {
name: 'CustomComponent',
components: {
comp: CustomComponent,
},
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
render() {
return <comp ref="comp" {...this.$attrs}></comp>
},
created() {
this.initMethods();
},
methods: {
initMethods() {
const methods = DataHeader?.methods || {};
Object.keys(methods)?.map(function (mKey){
this[mKey] = (...args) => {
return this.$refs.comp[mKey](...args);
}
}, this)
}
}
};
</script>