问题
Just starting Vue.js and webpack. I'm trying to add vue-chartjs functionality to my project. I'm receiving the following error:
Uncaught TypeError: _vueChartjs.Line.extend is not a function
at Object.defineProperty.value (..\..\CommodityChart.vue:5)
at __webpack_require__ (bootstrap 7040a42393b737f78245:659)
at fn (bootstrap 7040a42393b737f78245:85)
at Object.<anonymous> (CommodityChart.vue:3)
at __webpack_require__ (bootstrap 7040a42393b737f78245:659)
at fn (bootstrap 7040a42393b737f78245:85)
at Object.defineProperty.value (..\..\fetch-data.vue:36)
at __webpack_require__ (bootstrap 7040a42393b737f78245:659)
at fn (bootstrap 7040a42393b737f78245:85)
at Object.<anonymous> (fetch-data.vue:7)
in my package.json
"dependencies": {
"axios": "^0.15.3",
"bootstrap-vue": "^1.0.0-beta.9",
"chart.js": "^2.7.1",
"core-js": "^2.4.1",
"font-awesome": "^4.6.3",
"vue": "^2.1.8",
"vue-chartjs": "^3.0.0",
"vue-router": "^2.1.1",
"vue-server-renderer": "^2.1.8",
"vue-template-compiler": "^2.1.8",
"vuex": "^2.1.1",
"vuex-router-sync": "^4.0.1"
},
I can see the dependencies in my node_modules folder (chart.js and vue-chartjs).
my .vue file that is throwing the error looks like:
<script>
//Importing Line class from the vue-chartjs wrapper
import { Line } from 'vue-chartjs'
//Exporting this so it can be used in other components
export default Line.extend({
data () {
return {
datacollection: {
//Data to be represented on x-axis
labels: ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
datasets: [
{
label: 'Data One',
backgroundColor: '#f87979',
pointBackgroundColor: 'white',
borderWidth: 1,
pointBorderColor: '#249EBF',
//Data to be represented on y-axis
data: [40, 20, 30, 50, 90, 10, 20, 40, 50, 70, 90, 100]
}
]
},
//Chart.js options that controls the appearance of the chart
options: {
scales: {
yAxes: [{
ticks: {
beginAtZero: true
},
gridLines: {
display: true
}
}],
xAxes: [ {
gridLines: {
display: false
}
}]
},
legend: {
display: true
},
responsive: true,
maintainAspectRatio: false
}
}
},
mounted () {
//renderChart function renders the chart with the datacollection and options object.
this.renderChart(this.datacollection, this.options)
}
})
</script>
Do i need to import/reference the chart libraries somewhere else in my entry js file? Webpack references? Project is working fine without the chart.vue file.
回答1:
Syntax for creating chart component has been changed in the latest version (3.0.0
) of vue-chartjs, hence the error occurred.
According to the new syntax, you should create your chart component as follows :
import { Line } from 'vue-chartjs';
export default {
extends: Line,
data() {
return {
datacollection: {
//Data to be represented on x-axis
labels: ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
datasets: [{
label: 'Data One',
backgroundColor: '#f87979',
pointBackgroundColor: 'white',
borderWidth: 1,
pointBorderColor: '#249EBF',
//Data to be represented on y-axis
data: [40, 20, 30, 50, 90, 10, 20, 40, 50, 70, 90, 100]
}]
},
//Chart.js options that controls the appearance of the chart
options: {
scales: {
yAxes: [{
ticks: {
beginAtZero: true
},
gridLines: {
display: true
}
}],
xAxes: [{
gridLines: {
display: false
}
}]
},
legend: {
display: true
},
responsive: true,
maintainAspectRatio: false
}
}
},
mounted() {
//renderChart function renders the chart with the datacollection and options object.
this.renderChart(this.datacollection, this.options)
}
}
For more info, refer to the official documentation.
回答2:
This is how i have added line chart in my vue.js and laravel project.The work i have done is to count complete tasks for every month and show them in a line chart graph.
First you have to setup components for line chart in components directory.Save the file named as LineChart.js. This folder will contain:
import { Line, mixins } from 'vue-chartjs'
const { reactiveProp } = mixins
export default {
extends: Line,
mixins: [reactiveProp],
props: ['options'],
mounted () {
// this.chartData is created in the mixin.
// If you want to pass options please create a local options object
this.renderChart(this.chartData, this.options)
}
}
Then you have to write code into your vue file.
<div class="display">
<line-chart :chart-data="datacollection"></line-chart>
mounted() {
this.LoadCompleteTaskbyMonth();
}
data() {
return {
months: null,
month_data: null,
datacollection: null
}
}
methods:{
LoadCompleteTaskbyMonth()
{
this.$Helpers.Get('task-complete', {},this.$store.state.token)
.then((response) => {
this.months = response.map(a => a.month);
this.month_data = response.map(a => a.value);
this.datacollection = {
labels:this.months,
datasets: [
{
label: 'Completed Task',
data:this.month_data
}
]
}
})
}
}
And this is the query function:
public function completeTask()
{
$data = array();
$data = DB::table('tasks')
->select(
DB::raw("DATE_FORMAT(tasks.created_at,'%Y-%m') AS month2"),
DB::raw("DATE_FORMAT(tasks.created_at,'%Y-%M') AS month"),
DB::raw('count(*) AS value')
)
->groupBy('month','month2')
->orderBy('month2','ASC')
->where('task_status_id', '=', 5)
->get();
return response()->json($data);
}
NOTE:task-complete is an URL
来源:https://stackoverflow.com/questions/47078430/vue-js-uncaught-typeerror-vuechartjs-line-extend-is-not-a-function