我正在尝试在 Vue.js(与 vuex 一起用于状态管理)项目中使用vega-embed 。基本上,后端提供一个 Vega json 对象,前端通过带有点击事件的 HTTP GET 请求获取该对象。但是,我必须单击两次才能显示绘图,并且第一次单击事件总是会触发错误“ Uncaught (in promise) TypeError: Cannot read property '$schema' of null
”。有人可以帮我调试吗?非常感谢。详情如下图:
vue组件文件:
<template>
<button @click.native="fetchCars(); displayVegaPlot()">fetch cars</button>
<div id="vega-example"></div>
</template>
<script>
import {default as vegaEmbed} from 'vega-embed'
import {
mapState
} from 'vuex'
export default {
name: 'VegaExample',
props: {
component_msg: String
},
methods: {
fetchCars () {
this.$store.dispatch('fetchCars')
},
displayVegaPlot () {
vegaEmbed('#vega-example', this.vega_cars, {actions: false})
}
},
computed: {
...mapState([
'vega_cars'
])
}
}
</script>
...和商店js文件:
import Vue from 'vue'
import Vuex from 'vuex'
import axios from 'axios'
Vue.use(Vuex)
export default new Vuex.Store({
strict: true,
state: {
error: '',
vega_cars: null
},
mutations: {
SET_CARS: (state, cars) => {
state.vega_cars = cars
},
SET_ERROR: (state, error) => {
state.error = error
}
}
actions: {
fetchCars: (context) => {
axios.get(`vega_cars`)
.then(response => context.commit('SET_CARS', response.data))
.catch(error => context.commit('SET_ERROR', error))
}
}