Skip to content

Instantly share code, notes, and snippets.

@olegkorol
Forked from rvanzon/nuxt.config.js
Created January 31, 2018 15:10
Show Gist options
  • Save olegkorol/7d6c5464658e1755891819164b334443 to your computer and use it in GitHub Desktop.
Save olegkorol/7d6c5464658e1755891819164b334443 to your computer and use it in GitHub Desktop.
A way to use vue-chartjs as a plugin of Nuxt.js

How does this work

  1. Create a custom plugin and put it in plugins (plugins_vue-chartjs.js).
  2. Add the plugin to nuxt.config.js and set ssr to false to prevent the server to initialize it.
  3. You can use the component now just like other Vue-components. The only problem is that you get errors because the DOM-tree is out of sync (because the server misses the component)
  4. As mounted() is only called by the client (browser) we're using this to render the component only in the browser. Add showLine: false to data (so the server will not render it) and turn it to true in mounted().
  5. Use v-if to render the component. Now it won't be rendered on the server side, but it will show up in de browser.

This way also works with other Vue.js plugins

Used in this example:
npm install vue-chartjs --save
npm install chart.js --save

// just an example. A cleaner way is to wrap the showLine-stuff in a dedicated component
<template>
<div>
<my-line v-if="showLine" :data="lineData" :options="options">
</div>
</template>
<script>
export default {
data () {
return {
showLine: false
}
},
mounted () {
this.showLine = true // showLine will only be set to true on the client. This keeps the DOM-tree in sync.
},
asyncData () {
const lineData = {} // some data
const options = {} // some options
return { lineData, options }
}
}
</script>
...
/*
** Build configuration
*/
plugins: [
{ src: '~plugins/vue-chartjs.js', ssr: false },
],
...
import Vue from 'vue'
import { Line } from 'vue-chartjs'
Vue.component('my-line', Line.extend({
props: ['data', 'options'],
mounted () {
this.renderChart(this.data, this.options)
}
}))
@RouillerRomain
Copy link

hello,
thanks for that, It's actually doest work unfortunately for my Nuxt project, i got this error :
Uncaught TypeError: WEBPACK_IMPORTED_MODULE_1_vue_chartjs.a.extend is not a function

I simply pasted your code here,,

thanks,

@dev7ch
Copy link

dev7ch commented Mar 18, 2019

import Vue from "vue"
import { Line } from "vue-chartjs"

Vue.component("chart-line", {
  extends: Line,
  props: ["data", "options"],
  mounted() {
    this.renderChart(this.data, this.options)
  }
})

@luixal
Copy link

luixal commented Nov 11, 2020

I needed to use some different chart types and wasn't feeling like copy&pasting everything for each chart type and having to refactor each copy, etc... so I wrote this, in case someone finds it useful:

import Vue from 'vue'
import { Bar, Doughnut, Line, Pie } from 'vue-chartjs'

const registerComponent = function(name, originalComponent) {
    Vue.component(
        name,
        {
            extends: originalComponent,
            props: ['data', 'options'],
            mounted() {
                this.renderChart(this.data, this.options)
            }
        }
    )
}

registerComponent('BarChart', Bar);
registerComponent('DoughnutChart', Doughnut);
registerComponent('LineChart', Line);
registerComponent('PieChart', Pie);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment