question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

this.$refs doesn't seem to have expected canvas property

See original GitHub issue

Expected Behavior

Expect the example chart to render in the browser.

Actual Behavior

Error in console and nothing renders to the page.

Error occurs in the Bar [Vue warn]: Error in mounted hook: "TypeError: Cannot read property 'getContext' of undefined"

TypeError: Cannot read property 'getContext' of undefined
    at VueComponent.renderChart (Bar.js?7b87:78)
    at VueComponent.boundFn [as renderChart] (vue.runtime.esm.js?ff9b:182)
    at VueComponent.mounted (win-ew-pie-chart.vue?6dcc:15)
    at callHook (vue.esm.js?efeb:2665)
    at Object.insert (vue.esm.js?efeb:3526)
    at invokeInsertHook (vue.esm.js?efeb:5469)
    at Vue$3.patch [as __patch__] (vue.esm.js?efeb:5639)
    at Vue$3.Vue._update (vue.esm.js?efeb:2414)
    at Vue$3.updateComponent (vue.esm.js?efeb:2538)
    at Watcher.get (vue.esm.js?efeb:2881)

My Vue Component

<template>
<div class="app">

</div>
</template>

<script>
import { Bar } from 'vue-chartjs'

// console.log(Bar)
export default Bar.extend({
  mounted () {
    // Overwriting base render method with actual data.
    this.renderChart({
      labels: ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
      datasets: [
        {
          label: 'GitHub Commits',
          backgroundColor: '#f87979',
          data: [40, 20, 12, 39, 10, 40, 39, 80, 40, 20, 12, 11]
        }
      ]
    })
  }
})
</script>

<style lang="css">
</style>

Environment

  • vue.js version: 2.4.2
  • vue-chart.js version: 2.7.2
  • webpack version: 3.3.0
  • yarn version: 0.27.5

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:5
  • Comments:9 (3 by maintainers)

github_iconTop GitHub Comments

103reactions
aperturelesscommented, Jul 23, 2017

You have to remove the template part of your component.

You are extending the baseChart component, which comes with a template. If you keep the

<template>
<div class="app">

</div>
</template>

you overwrite everything from the base chart, as templates can not be merged properly.

19reactions
l-zhicommented, Jul 24, 2017

Just modify the template like this , may fixed the error

<template> <div class="container"> <canvas ref="canvas" width="900" height="400"></canvas> </div> </template>

Read more comments on GitHub >

github_iconTop Results From Across the Web

Vue3 TypeError: template ref.value is null - Stack Overflow
So I have to do something after initialization, right? Here is my code: <template> <canvas ref="chartRef" /> ...
Read more >
How to use React createRef - LogRocket Blog
Learn how to use the React.createRef() method and useRef Hook in React to simplify creating refs and interact with the HTML DOM.
Read more >
The Embed Audio element - HTML - MDN Web Docs - Mozilla
This enumerated attribute indicates whether to use CORS to fetch the related audio file. CORS-enabled resources can be reused in the <canvas> ...
Read more >
The Book Thief - SharpSchool
I've seen more eclipses than I care to remember. ... The Hubermanns lived in one of the small, boxlike houses on Himmel Street....
Read more >
Three Insights I Gained While Researching Vue.js Accessibility
Web app accessibility appears difficult because it seems that there is little information on the subject available online. Let's take a ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found