问题描述
今天在写echarts图标时候封装成一个vue组件的时候发现一个诡异的错误,怎么都找不到原因
问题出现的环境背景及自己尝试过哪些方法
初步怀疑是异步的问题
相关代码
if(true){
let myChart = echarts.init(document.getElementById('myChart1'));
}else{
let myChart = echarts.init(document.getElementById('myChart2'));
}
console.log(myChart)
当我改变下方式就又可以了
if(true){
let myChart = echarts.init(document.getElementById('myChart1'));
console.log(myChart)
}else{
let myChart = echarts.init(document.getElementById('myChart2'));
}
楼主应该了解下JS基础知识,这不是异步的问题。
你在一个if语句里用let定义了myChart变量,那么这个变量就只能在它所在的代码块里被访问到,也就是if里面才能访问到,外部访问需要闭包或者定义全局变量。