Question one:
ERROR
Cannot read properties of undefined (reading 'init') TypeError: Cannot read properties of undefined (reading 'init')
Error reason: echarts import method is wrong
Check my package.json file, echarts version is 5
Solution: Change the introduction method of echarts4 from 'echarts' to the introduction method of echarts5 in the component
import * as echarts from 'echarts'
Question two:
ERROR
Initialize failed: invalid dom.
Reason for error: Invalid dom. It is because the dom has not been loaded in the code, and the initialization of echarts has already started
<script lang="ts" setup>
// echarts5的引用方式
import * as echarts from 'echarts'
const myChart = echarts.init(document.getElementById("main"))
let option = {
xAxis: {
type: 'category',
data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
},
yAxis: {
type: 'value'
},
series: [
{
data: [120, 200, 150, 80, 70, 110, 130],
type: 'bar',
showBackground: true,
backgroundStyle: {
color: 'rgba(180, 180, 180, 0.2)'
}
}
]
};
myChart.setOption(option)
</script>
Solution: Put the application of echarts into onMounted(), or put the application of echarts into the timer, delay loading
<script lang="ts" setup>
import { onMounted } from 'vue'
// echarts5的引用方式
import * as echarts from 'echarts'
onMounted( ()=>{
const myChart = echarts.init(document.getElementById("main"))
let option = {
xAxis: {
type: 'category',
data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
},
yAxis: {
type: 'value'
},
series: [
{
data: [120, 200, 150, 80, 70, 110, 130],
type: 'bar',
showBackground: true,
backgroundStyle: {
color: 'rgba(180, 180, 180, 0.2)'
}
}
]
};
myChart.setOption(option)
})
</script>