Skip to content

Commit a745553

Browse files
committed
fix: use dynamic import for the apex charts
1 parent 87aff58 commit a745553

5 files changed

Lines changed: 58 additions & 15 deletions

File tree

adminforth/spa/src/afcl/AreaChart.vue

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
</template>
44

55
<script setup lang="ts">
6-
import ApexCharts, { type ApexOptions } from 'apexcharts';
6+
import type ApexCharts from 'apexcharts';
7+
import { type ApexOptions } from 'apexcharts';
78
import { ref, type Ref, watch, computed, onUnmounted } from 'vue';
9+
type ApexChartsConstructor = new (el: Element, options: any) => ApexCharts;
810
911
const chart: Ref<HTMLDivElement | null> = ref(null);
1012
@@ -137,15 +139,22 @@ const options = computed(() => {
137139
});
138140
139141
let apexChart: ApexCharts | null = null;
142+
let ApexChartsCtor: ApexChartsConstructor | null = null;
140143
141-
watch(() => [options.value, chart.value], (value) => {
144+
watch(() => [options.value, chart.value], async (value) => {
142145
if (!value || !chart.value) {
143146
return;
144147
}
148+
149+
if (!ApexChartsCtor) {
150+
const module = await import('apexcharts') as { default: ApexChartsConstructor };
151+
ApexChartsCtor = module.default;
152+
}
153+
145154
if (apexChart) {
146155
apexChart.updateOptions(options.value);
147156
} else {
148-
apexChart = new ApexCharts(chart.value, options.value);
157+
apexChart = new ApexChartsCtor(chart.value, options.value);
149158
apexChart.render();
150159
}
151160
});

adminforth/spa/src/afcl/BarChart.vue

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
</template>
44

55
<script setup lang="ts">
6-
import ApexCharts, { type ApexOptions } from 'apexcharts';
6+
import type ApexCharts from 'apexcharts';
7+
import { type ApexOptions } from 'apexcharts';
78
import { ref, type Ref, watch, computed, onUnmounted } from 'vue';
9+
type ApexChartsConstructor = new (el: Element, options: any) => ApexCharts;
810
911
const chart: Ref<HTMLDivElement | null> = ref(null);
1012
@@ -150,15 +152,22 @@ const options = computed(() => {
150152
});
151153
152154
let apexChart: ApexCharts | null = null;
155+
let ApexChartsCtor: ApexChartsConstructor | null = null;
153156
154-
watch(() => [options.value, chart.value], (value) => {
157+
watch(() => [options.value, chart.value], async (value) => {
155158
if (!value || !chart.value) {
156159
return;
157160
}
161+
162+
if (!ApexChartsCtor) {
163+
const module = await import('apexcharts') as { default: ApexChartsConstructor };
164+
ApexChartsCtor = module.default;
165+
}
166+
158167
if (apexChart) {
159168
apexChart.updateOptions(options.value);
160169
} else {
161-
apexChart = new ApexCharts(chart.value, options.value);
170+
apexChart = new ApexChartsCtor(chart.value, options.value);
162171
apexChart.render();
163172
}
164173
});

adminforth/spa/src/afcl/MixedChart.vue

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
</template>
44

55
<script setup lang="ts">
6-
import ApexCharts, { type ApexOptions } from 'apexcharts';
6+
import type ApexCharts from 'apexcharts';
7+
import { type ApexOptions } from 'apexcharts';
78
import { ref, onUnmounted, watch, computed } from 'vue';
9+
type ApexChartsConstructor = new (el: Element, options: any) => ApexCharts;
810
911
const props = defineProps<{
1012
data: {
@@ -103,17 +105,23 @@ const chartOptions = computed(() => {
103105
});
104106
105107
let apexChart: ApexCharts | null = null;
108+
let ApexChartsCtor: ApexChartsConstructor | null = null;
106109
107110
108-
watch(() => [chartOptions.value, chart.value], ([newOptions, newRef]) => {
111+
watch(() => [chartOptions.value, chart.value], async ([newOptions, newRef]) => {
109112
if (!newOptions || !newRef) {
110113
return;
111114
}
115+
116+
if (!ApexChartsCtor) {
117+
const module = await import('apexcharts') as { default: ApexChartsConstructor };
118+
ApexChartsCtor = module.default;
119+
}
112120
113121
if (apexChart) {
114122
apexChart.updateOptions(newOptions);
115123
} else if (chart.value) {
116-
apexChart = new ApexCharts(chart.value, newOptions);
124+
apexChart = new ApexChartsCtor(chart.value, newOptions);
117125
apexChart.render();
118126
}
119127
}, { deep: true });

adminforth/spa/src/afcl/PieChart.vue

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
</template>
44

55
<script setup lang="ts">
6-
import ApexCharts, { type ApexOptions } from 'apexcharts';
6+
import type ApexCharts from 'apexcharts';
7+
import { type ApexOptions } from 'apexcharts';
78
import { ref, type Ref, watch, computed, onUnmounted } from 'vue';
9+
type ApexChartsConstructor = new (el: Element, options: any) => ApexCharts;
810
911
const chart: Ref<HTMLDivElement | null> = ref(null);
1012
@@ -154,15 +156,22 @@ const options = computed(() => {
154156
});
155157
156158
let apexChart: ApexCharts | null = null;
159+
let ApexChartsCtor: ApexChartsConstructor | null = null;
157160
158-
watch(() => [options.value, chart.value], (value) => {
161+
watch(() => [options.value, chart.value], async (value) => {
159162
if (!value || !chart.value) {
160163
return;
161164
}
165+
166+
if (!ApexChartsCtor) {
167+
const module = await import('apexcharts') as { default: ApexChartsConstructor };
168+
ApexChartsCtor = module.default;
169+
}
170+
162171
if (apexChart) {
163172
apexChart.updateOptions(options.value);
164173
} else {
165-
apexChart = new ApexCharts(chart.value, options.value);
174+
apexChart = new ApexChartsCtor(chart.value, options.value);
166175
apexChart.render();
167176
}
168177
});

adminforth/spa/src/afcl/TreeMapChart.vue

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
</template>
44

55
<script setup lang="ts">
6-
import ApexCharts, { type ApexOptions } from 'apexcharts';
6+
import type ApexCharts from 'apexcharts';
7+
import { type ApexOptions } from 'apexcharts';
78
import { ref, type Ref, watch, computed, onUnmounted } from 'vue';
9+
type ApexChartsConstructor = new (el: Element, options: ApexOptions) => ApexCharts;
810
911
const chart: Ref<HTMLDivElement | null> = ref(null);
1012
@@ -94,16 +96,22 @@ const options = computed(() => {
9496
});
9597
9698
let apexChart: ApexCharts | null = null;
99+
let ApexChartsCtor: ApexChartsConstructor | null = null;
97100
98-
watch(() => [options.value, chart.value], (value) => {
101+
watch(() => [options.value, chart.value], async (value) => {
99102
if (!value || !chart.value) {
100103
return;
101104
}
102105
106+
if (!ApexChartsCtor) {
107+
const module = await import('apexcharts') as { default: ApexChartsConstructor };
108+
ApexChartsCtor = module.default;
109+
}
110+
103111
if (apexChart) {
104112
apexChart.updateOptions(options.value);
105113
} else {
106-
apexChart = new ApexCharts(chart.value, options.value);
114+
apexChart = new ApexChartsCtor(chart.value, options.value);
107115
apexChart.render();
108116
}
109117
});

0 commit comments

Comments
 (0)