Skip to content

Instantly share code, notes, and snippets.

@rvanzon
Last active September 1, 2022 13:25
Show Gist options
  • Save rvanzon/096132b7b46be43659cf26360c664e9a to your computer and use it in GitHub Desktop.
Save rvanzon/096132b7b46be43659cf26360c664e9a to your computer and use it in GitHub Desktop.
A way to use vue-chartjs as a plugin of Nuxt.js

(WARNING: THIS IS OUTDATED, DON'T USE AS IS! INSTEAD CHECK OUT THE COMMENTS AT THE BOTTOM)

And check out: https://github.com/nuxt/nuxt.js/tree/dev/examples/vue-chartjs

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-chart.js', mode: 'client' },
],
...
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)
}
}))
@nuno-studiographene
Copy link

nuno-studiographene commented Apr 7, 2021

MarcelloTheArcane Thanks a lot! I spent 3 hours trying to figure out why it wasnt working. Your solution fixed my WEBPACK_IMPORTED_MODULE issue

Copy link

ghost commented Apr 8, 2021

@MarcelloTheArcane Thanks Dude. I spent a lot of time why WEBPACK_IMPORTED_MODULE happening I couldn't find anything. I solved

@isebarn
Copy link

isebarn commented Apr 8, 2021

For Nuxt > 2.9.0 you should use mode: 'client' in the nuxt.config.js:

 plugins: [
    {
      src: '~/plugins/vue-chart.js', mode: 'client'
    }
  ]

And to render it you should use a <client-only> tag

<client-only>
    <my-line :data="data"></my-line>
</client-only>

Thanks!

If anyone's getting the "export 'default' (imported as 'Chart') was not found in 'chart.js' error or WEBPACK_IMPORTED_MODULE errors, downgrade your chart.js version to ^2.9.3.

Thanks!

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