[英]VueJs/ChartJs - single file component computed property only renders when I click the component in Vue Dev Tools
I have a single file component that uses Chart.Js to render a simple visualization for some hard coded data. 我只有一个文件组件,该组件使用Chart.Js来呈现一些硬编码数据的简单可视化效果。 I'm calling the Chart.Js library through a CDN in the head
section of my index.html. 我正在通过index.html head
的CDN调用Chart.Js库。
I'm using the official Webpack template. 我正在使用官方的Webpack模板。
For some reason, the chart won't render unless I click on the component inside the Vue Dev Tools extension. 由于某些原因,除非我单击Vue Dev Tools扩展内的组件,否则无法显示图表。
I've tried changing it from computed to created/mounted and that hasn't worked. 我尝试将其从计算更改为已创建/已安装,但这没有用。
Here's my code. 这是我的代码。 Any help getting this to render properly would be much appreciated. 任何帮助使它正确呈现将不胜感激。
<template lang="html">
<div>
<div class="row">
<div class="col-sm-12 col-md-4">
<canvas id="carbChart"></canvas>
</div>
</div>
</div>
</template>
<script>
export default {
data() {
return {
calories: 3000,
childSex: 'male',
childAge: 'eighteen'
}
},
computed: {
nutrientCharts: function() {
let calories = this.calories;
let carbCtx = document.getElementById('carbChart').getContext('2d');
let carbChart = new Chart(carbCtx, {
type: 'doughnut',
data: {
labels: ['Low', 'Good', 'Too Much'],
datasets: [{
label: 'carbs',
data: [calories * .45, calories * .65, calories * 1],
backgroundColor: ['orange', 'blue', 'red']
}]
}
});
}
}
}
</script>
You have defined the method in a computed
property but never used it. 您已在computed
属性中定义了该方法,但从未使用过。
Assuming you want to just get this running, load the chart on mounted: 假设您只是想让它运行,请在已挂载的图表上加载:
mounted() {
this.nutrientCharts();
},
methods: {
nutrientCharts: function () {
// your code here
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.