json - 无法在 Vue 中的 HighCharts 回调中附加到 JS 对象
问题描述
我正在使用 Vue 3.9.3 和highcharts-vue 1.3.5。我试图在加载时将每个高图放入一个对象中,这样我就可以访问我想要的任何图表
myCharts.aChart
或者
myCharts.anotherChart
,
所以我可以轻松做到myCharts.anotherChart.addData
在实践中,我这样做,
<highcharts :constructor-type="'stockChart'" :options="options" :callback="chartcallback" ></highcharts>
接着
data(){
return{
charts:{}
}
},
methods:{
chartcallback(hc){
let obj = {[hc.options.id] : hc};
this.charts = Object.assign(this.charts, obj);
//also tried this
const newChart = {id:hc.options.id, chart : hc};
this.$set(this.charts, newChart.id, newChart);
//also tried this
this.charts = Object.assign({}, this.charts, {
[hc.options.id] : hc
});
//also tried this
this.charts[hc.options.id]= hc;
console.log('test ', this.charts);
}
}
然后我会watch
我的数据,每次它们发生变化时,我都会将数据添加到每个高图,
watch: {
myData: {
immediate: true,
deep: true,
handler(){
this.charts.NameChart.series[0].setData(this.myData[0], true);
this.charts.DrinkChart.series[0].setData(this.myData[1], true);
//etc....
this.charts.NameChart
或者this.charts.DrinkChart
应该在 NameChart 中构造chartcallback
,DrinkChart 的值hc.options.id
hc.options.id
总是有一个值,它就在那里,我检查了。
问题是
在chartcallback
我做的最后console.log('test ', this.charts);
,产生的 obj 是
//first time
{NameChart:chart}
//second time
{NameChart:chart}
但应该是
//first time
{NameChart:chart}
//second time
{NameChart:chart,
DrinkChart:chart}
看起来它会覆盖this.charts
每次chartcallback
调用。正如我所指出的,我尝试了几种方法,但没有任何效果。
我能做些什么 ?
谢谢
解决方案
推荐阅读
- ruby-on-rails - 警告:命令行选项“-Wimplicit-int”对 C/ObjC 有效,但对 C++ 无效
- kubernetes - 负载均衡器后面的请求
- php - fgetcsv 错误地解释数字
- spring - 将字符串转换为 LocalDateTime
- vmware-clarity - Clarity 是否提供带有右侧图标的手风琴
- c# - data-ajax-update 在 dotnet core jquery unobtrusive ajax 中不起作用
- azure-devops - 如何获取管理员用户 kubeconfig
- apache - 如何检查.htaccess中是否未定义变量?
- javascript - 使用mysql的带有子行的数据表
- svn2git - 使用 kde svn2git 工具转换为 git 时保留 svn 复制历史记录