gpt4 book ai didi

javascript - 如何从对象内部访问 vuejs 方法? (Vuejs 2)

转载 作者:行者123 更新时间:2023-12-03 02:49:02 28 4
gpt4 key购买 nike

我想从 Chart 对象访问 getValue 方法,但我得到的函数未定义。

<template>
<div>
<canvas width="600" height="400" ref="canvas"></canvas>
</div>
</template>
<script>
import Vue from 'vue';
import Chart from 'chart.js';
import Axios from 'axios';

export default {
mixins: [DateRangeMixin],
props: {
// other props...
callback: false,
},

data() {
return {
chart: '',
};
},

mounted() {
// ...
},

methods: {

//other methods...,
getValue(data) {
if (data === 1) {
return 'Up'
} else if(data === 0) {
return 'Down';
}
},
render(data) {
this.chart = new Chart(this.$refs.canvas, {
type: 'line',
data: {

labels: Object.keys(data),
datasets: [{
// a lot of data ....
data: Object.values(data),

}]

},
options: {
scales: {
yAxes: [{
ticks: {
beginAtZero: true,
callback(label, index, labels) {
return this.getValue(label); // <-- Tried this and got: 'this.getValue is not a function'. I understand it bounces to new Chart object, but how to resolve this?
}
}
}]
}
}

});

},
},
};
</script>

我知道这是因为 Chart 是一个对象,并且 this 指向它,但是我如何解决这个问题并从回调中访问我的方法?

我想如果导出默认值...将设置为变量,那么我可以通过variable.methods.getValue访问我的方法code> ,但在这种情况下我怎样才能实现我的目标?

最佳答案

在创建new Chart()之前,将其分配给变量self:var self = this;。然后,您可以通过self访问组件属性。

关于javascript - 如何从对象内部访问 vuejs 方法? (Vuejs 2),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47968121/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com