面试篇-vue中第三方库的使用(echarts)

文章目录

Echarts

基本用法:

  • 初始化类: echarts.init(domId)
  • 样式配置: option={}
  • 渲染图展: myChart.setOption(option)

一、vue2和vue3中使用

1、安装echarts

bash 复制代码
npm install echarts --save

vue2中引入与 使用

main.js引入

js 复制代码
import Vue from "vue";
import App from "./App.vue";
import * as echarts from "echarts";
Vue.prototype.$echarts = echarts;

vue页面中使用

html 复制代码
<div id="myChart" style="width: 500px;height: 500px"></div>
 
<script>
export default{
  mounted() {
      let myChart = this.$echarts.init(document.getElementById("myChart"));
      myChart.setOption({
        tooltip: {},
        xAxis: {
            data: ['衬衫', '羊毛衫', '雪纺衫', '裤子', '高跟鞋', '袜子']
        },
        yAxis: {},
        series: [
          {
            name: "销量",
            type: "bar",
            data: [5, 20, 36, 10, 10, 20, 20, 36, 10, 10, 20],
          },
        ]
      });
  },
}
</script>

vue3中引入与 使用

引入方法一: 通过getCurrentInstance全局引入
main.js引入

js 复制代码
import { createApp } from 'vue'
import * as echarts from 'echarts'   //主要代码
import App from './App.vue'
const app =  createApp(App)
app.config.globalProperties.$echarts = echarts      //主要代码
app.mount('#app')

vue页面中使用

html 复制代码
<div id="myChart" style="width: 200px; height:300px; "></div>
 
<script lang="ts" setup>
const echarts = getCurrentInstance()?.appContext.config.globalProperties.$echarts;
 
function as() {
  const myChart = echarts.init(document.getElementById("myChart"));
  // 绘制图表
  myChart.setOption({
    tooltip: {},
    xAxis: {
      data: ["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"],
    },
    yAxis: {},
    series: [
      {
        name: "销量",
        type: "bar",
        data: [5, 20, 36, 10, 10, 20, 20, 36, 10, 10, 20],
      },
    ],
  });
}
onMounted(() => {
 
  nextTick(() => {
    as();
  });
});
</script>

引入方法二: 仅在组件内部引入及使用

html 复制代码
<script lang="ts" setup>
import * as echarts from 'echarts'
 
function as() {
  const myChart = echarts.init(document.getElementById("myChart"));  //主要语句
 
  myChart.setOption({
    tooltip: {},
    xAxis: {
      data: ["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"],
    },
    yAxis: {},
    series: [
      {
        name: "销量",
        type: "bar",
        data: [5, 20, 36, 10, 10, 20, 20, 36, 10, 10, 20],
      },
    ],
  });
}
 
onMounted(() =>{
  nextTick(() => {
     
  })
});
 
</script>

二、相关面试题

1、vue2 vue3使用echarts无法二次渲染、echarts无法渲染、echarts图标不显示

销毁div重新渲染:

js 复制代码
$("#myChart").removeAttr("_echarts_instance_");
// 或
$("#myChart").removeAttr("_echarts_instance_").empty();

2、切换其他组件统计图时,出现卡顿如何解决

卡顿的原因: 每个图例在没有数据时 会创建一个定时器去渲染气泡 ,页面切换后,图例销毁了 ,但echarts实例还在内存 ,气泡渲染定时器仍在运行 ,导致echarts占用CPU高,卡顿

解决方案mounteddestroy间加一个beforeDestroy释放该页面的echart资源

js 复制代码
mounted(){}
beforeDestroy(){
	//清空图例数据,不影响图例resize,释放内存
	this.chart.clear()
}
destroy(){}

3、echarts图标自适应div问题

div发生resize事件 时,让其触发echarts的resize事件

javascript 复制代码
$('.chart').resize(function(){
	myChart.resize()
})
//若jquery的resize不生效,引入`jquery.ba-resize.js`
相关推荐
luanma1509801 小时前
Spring 框架——@Retryable 注解与 @Recover 注解
java·前端·spring
llxxyy卢1 小时前
polar-web部分中等题目
android·前端·sql·web安全
非凡ghost2 小时前
Zen Browser:基于 Firefox 的极简开源浏览器,隐私与速度兼得
前端·网络·windows·学习·开源·firefox·软件需求
ivwsjc2 小时前
vue3 echarts地图点到点之间的飞线图
前端·javascript·vue·echarts
小李子呢02112 小时前
JS中的Set 核心认知
前端·javascript·es6
程序员阿耶2 小时前
【前端面试知识点】CSS contain 属性如何用于性能优化?它有哪些可选值及作用?
前端·面试
阳火锅2 小时前
34岁前端倒计时:老板用AI手搓系统那天,我知道我的“体面退休”是个笑话
前端·后端·程序员
姓王者2 小时前
# 解决 Nautilus 自定义终端插件安装依赖问题
前端·后端·全栈
宸翰2 小时前
在VS code中如何舒适的开发Python
前端·python