使用window.addEventListener方法
/*
在Vue中使用,加载动态页面
*/
mounted() {
// 1.设置echarts对象
this.myChart = this.$echarts.init(document.getElementById("dangerLevel"));
// 2.获取后台数据
api.dangerLevel().then(res => {
for (let key in res.data) {
//使用扩展运算符,添加新数据
this.people = [...this.people, res.data[key]];
}
// 3.重新设置echarts对象 why? echarts对象只初始化一次,数据变化,需要init新对象重新赋
this.myChart = this.$echarts.init(document.getElementById("dangerLevel"));
//封装的设置setOption方法
this.getCharts();
});
this.getCharts();
//让echarts图表根据随网页大小动态改变
window.addEventListener("resize", () => {
this.myChart.resize();
});
},