sách gpt4 ai đã đi

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

In lại 作者:行者123 更新时间:2023-12-03 02:49:02 29 4
mua khóa gpt4 Nike

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



import Vue from 'vue';
import Chart from 'chart.js';
import Axios from 'axios';

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

dữ liệu() {
trở lại {
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?
}
}
}]
}
}

});

},
},
};

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

我想如果Xuất mặc định...将设置为biến đổi,那么我可以通过variable.methods.getValue访问我的方法code> ,但在这种情况下我怎样才能实现我的目标?

1 Câu trả lời

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

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

29 4 0
行者123
Hồ sơ cá nhân

Tôi là một lập trình viên xuất sắc, rất giỏi!

Nhận phiếu giảm giá Didi Taxi miễn phí
Mã giảm giá Didi Taxi
Giấy chứng nhận ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com