如何使用Vue-Chartjs发出活动?
我使用VUE JS,并显示带有Chartjs的图形。当我单击图表时,我希望发出一个事件以获取父组件中的数据。我的onclick功能有效,但事件不起作用。 你对我的问题有想法吗? 组件线2.
<script>
import { mixins, Line } from "vue-chartjs";
const { reactiveProp } = mixins;
export default {
extends: Line,
mixins: [reactiveProp],
props: ["options"],
mounted() {
const self = this;
console.log(self);
this.options.onClick = function (e, tooltipItems) {
console.log(tooltipItems[0].__ob__); //.logged
self.$emit("sendIndex", tooltipItems[0]._index);
};
this.renderChart(this.chartData, this.options);
},
};
</script>
<style scoped></style>
我的主要组件
...
<h1 v-on:sendIndex="getIndexCoord($event)">{{ indexCoord }}</h1>
...
methods: {
getIndexCoord: function (e) {
console.log("rrr", e); //Not logged
this.indexCoord = e;
},
}
求职
I use vue js and I display a graph with chartjs. When I click on the graph I want emit an event for get data in parent component. My onClick function works but not the event.
Do you have an idea of my problem ?
Component Line2.vue
<script>
import { mixins, Line } from "vue-chartjs";
const { reactiveProp } = mixins;
export default {
extends: Line,
mixins: [reactiveProp],
props: ["options"],
mounted() {
const self = this;
console.log(self);
this.options.onClick = function (e, tooltipItems) {
console.log(tooltipItems[0].__ob__); //.logged
self.$emit("sendIndex", tooltipItems[0]._index);
};
this.renderChart(this.chartData, this.options);
},
};
</script>
<style scoped></style>
My main component
...
<h1 v-on:sendIndex="getIndexCoord($event)">{{ indexCoord }}</h1>
...
methods: {
getIndexCoord: function (e) {
console.log("rrr", e); //Not logged
this.indexCoord = e;
},
}
Regards
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
vue-chartjs
document 不描述处理图表事件,但是那里是示例。我们需要在图表上创建
Ref
,然后我们可以使用提供的getElementsateTateVent
函数来访问有关单击元素的信息。检查链接的示例以获取更多实用程序功能 - 在这里,我刚刚提供了简化的设置。PS:当我第一次尝试使用
[emagy&nbsp; emagy&nbsp; /code>我看到了错误
打字稿错误ts2590:表达式产生的联合类型太复杂而无法表示。
- 我升级到。
vue-chartjs
documentation doesn't describe handling chart events, but there is an example.We need to create a
ref
to our chart then we can use the providedgetElementsAtEvent
function to access information about the clicked elements. Check the linked example for more utility functions - here I have just provided a simplified setup.PS: When I first tried this with
[email protected]
I was seeing the errorTypeScript Error TS2590: Expression produces a union type that is too complex to represent.
- I upgraded to[email protected]
andvue-tsc@^2
and it fixed my type problem.1.首先创建eventbus.js文件
从“ vue”导入vue;
2.在您的char.js文件代码下方,如下
您要在该文件中访问该数据的位置,例如
1.first you create EventBus.js file
import Vue from 'vue';
2.In your char.js file code like below
where you want to access your data in that file like below