我是vue.js 2的新手。我目前正在使用vue框架重新创建此图: https://codepen.io/apexcharts/pen/rqWeBX
我需要从generateDayWiseTimeSeries()函数获取一些数据。 我目前正在从DATA()调用它,这导致我遇到此错误:
[Vue warn]: Error in data(): "ReferenceError: generateDayWiseTimeSeries is not defined"
我尝试使用.this语法返回数据,并将函数放置在created:部分中,但是没有运气。
这是我的vue.js代码,如果您能告诉我这是否是正确的方法,我将不胜感激。
Vue.component("apexchart", VueApexCharts);
const Dashboard = {
data: function() {
return {
series_5: [
generateDayWiseTimeSeries(new Date("22 Apr 2017").getTime(), 115, {
min: 30,
max: 90
})
]
};
},
components: {
apexcharts: VueApexCharts
},
methods: {
generateDayWiseTimeSeries: function(baseval, count, yrange) {
var i = 0;
var series = [];
while (i < count) {
var x = baseval;
var y =
Math.floor(Math.random() * (yrange.max - yrange.min + 1)) +
yrange.min;
series.push([x, y]);
baseval += 86400000;
i++;
}
return series;
}
},
,
template: `
// THE TEMPLATE IS USELESS FOR THIS PROBLEM
<div class="container">
<div class="row" >
<div class="col-sm col-xs-12">
<div class="card " style ="margin-bottom : 20px" >
<div class="card-body">
<h5 class="card-title">Indices de ventes</h5>
<p class="card-text">Ventes totales .</p>
<div><apexchart type="bar" :options="options_1" :series="series_1"></apexchart></div>
</div>
</div>
</div>
<div class="col-sm col-xs-12">
<div class="card " style ="margin-bottom : 20px" >
<div class="card-body">
<h5 class="card-title">Ventes</h5>
<p class="card-text">Indice des ventes total.</p>
<div><apexchart type="line" :options="options_2" :series="series_2"></apexchart></div>
</div>
</div>
</div>
</div> // THE TEMPLATE IS USELESS FOR THIS PROBLEM
`
};
编辑:已修改
series_5: [
this.generateDayWiseTimeSeries(new Date("22 Apr 2017").getTime(), 115, {
min: 30,
max: 90
})
]
现在它似乎正在工作!
答案 0 :(得分:0)
可以通过将this
添加到对generateDayWiseTimeSeries()
的呼叫中来解决您的问题。
series_5: [
this.generateDayWiseTimeSeries(new Date("22 Apr 2017").getTime(), 115, { min: 30, max: 90 })
]
它没有声明为全局函数,因此在调用它时需要提供上下文。
话虽如此,看来您正在寻找的是computed property。修改您的JavaScript,如下所示:
const Dashboard = {
data: function() {
return {};
},
components: {
apexcharts: VueApexCharts
},
computed: {
series_5() {
return this.generateDayWiseTimeSeries(new Date("22 Apr 2017").getTime(), 115, { min: 30, max: 90 });
}
},
methods: {
generateDayWiseTimeSeries: function(baseval, count, yrange) {
var i = 0;
var series = [];
while (i < count) {
var x = baseval;
var y =
Math.floor(Math.random() * (yrange.max - yrange.min + 1)) +
yrange.min;
series.push([x, y]);
baseval += 86400000;
i++;
}
return series;
}
},
,
...
}
通常,逻辑不属于data()
函数。按照惯例,data()
仅返回代表Vue实例数据的JSON对象。