dataease/frontend/src/components/AsyncComponent/index.vue

74 lines
1.5 KiB
Vue
Raw Normal View History

2021-05-11 16:32:21 +08:00
<template>
<component
:is="mode"
2022-02-17 18:39:44 +08:00
:ref="refId"
:obj="obj"
2021-05-11 16:32:21 +08:00
v-bind="$attrs"
v-on="$listeners"
/>
</template>
<script>
2022-02-17 18:39:44 +08:00
import { uuid } from 'vue-uuid'
import { get } from '@/api/system/dynamic'
2021-05-11 16:32:21 +08:00
export default {
name: 'AsyncComponent',
inheritAttrs: true,
props: {
// 父组件提供请求地址
url: {
type: String,
default: ''
2021-12-27 16:26:59 +08:00
},
obj: {
type: Object,
default: () => {}
2021-05-11 16:32:21 +08:00
}
},
data() {
return {
resData: '',
2022-02-17 18:39:44 +08:00
mode: '',
refId: null
2021-05-11 16:32:21 +08:00
}
},
watch: {
url: {
immediate: true,
async handler(val, oldVal) {
if (!this.url) return
// Cache 缓存 根据 url 参数
if (!window.SyncComponentCache) {
window.SyncComponentCache = {}
}
let res
if (!window.SyncComponentCache[this.url]) {
window.SyncComponentCache[this.url] = get(this.url)
// window.SyncComponentCache[this.url] = Axios.get(this.url)
2021-05-11 16:32:21 +08:00
res = await window.SyncComponentCache[this.url]
} else {
res = await window.SyncComponentCache[this.url]
}
2021-11-11 14:13:48 +08:00
const Fn = Function
this.mode = new Fn(`return ${res.data || res}`)()
/* if (res && res.data) {
2021-11-11 14:06:03 +08:00
const Fn = Function
this.mode = new Fn(`return ${res.data || res}`)()
2021-11-11 14:13:48 +08:00
} */
2021-05-11 16:32:21 +08:00
}
}
},
2022-02-17 18:39:44 +08:00
created() {
this.refId = uuid.v1
},
2021-05-11 16:32:21 +08:00
methods: {
2022-02-17 18:39:44 +08:00
chartResize() {
this.$refs[this.refId] && this.$refs[this.refId].chartResize && this.$refs[this.refId].chartResize()
}
2021-05-11 16:32:21 +08:00
}
}
</script>