1. <template>
    2. <div id="chart" :style="{ width: '600px', height: '600px' }"></div>
    3. </template>
    4. <script setup lang="ts">
    5. import { onMounted } from 'vue';
    6. import { EChartsOption, init } from 'echarts';
    7. // echarts在mounted时初始化,卸载mounted里面
    8. onMounted(() => {
    9. // 获取dom,断言HTMLElement类型,否则会报错
    10. const chartEle: HTMLElement = document.querySelector('#chart') as HTMLElement; //获取dom
    11. const chart = init(chartEle); //初始化echarts
    12. const option: EChartsOption = {
    13. xAxis: {
    14. type: 'category',
    15. data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'],
    16. },
    17. yAxis: {
    18. type: 'value',
    19. },
    20. series: [
    21. {
    22. data: [150, 230, 224, 218, 135, 147, 260],
    23. type: 'line',
    24. },
    25. ],
    26. }; // 配置项
    27. option && chart.setOption(option); // 设置配置项
    28. });
    29. </script>
    30. <style>
    31. #app {
    32. font-family: Avenir, Helvetica, Arial, sans-serif;
    33. -webkit-font-smoothing: antialiased;
    34. -moz-osx-font-smoothing: grayscale;
    35. text-align: center;
    36. color: #2c3e50;
    37. margin-top: 60px;
    38. }
    39. #chart {
    40. width: 600px;
    41. height: 400px;
    42. }
    43. </style>