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.

TypeError: this.$refs.canvas is undefined

See original GitHub issue

Expected Behavior

import the package, extend the Bar Chart and render it.

Actual Behavior

Using this package on a vuetify project, and extending the Bar chart, as the code example of the README, when i want to render the chart, it throws me an error:

[Vue warn]: Error in mounted hook: "TypeError: this.$refs.canvas is undefined"

Environment

  • vue.js version: 2.4.2
  • vue-chart.js version: 2.7.1
  • npm version: 5.5.1

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

11reactions
aperturelesscommented, Nov 13, 2017

Can you post your code?

I would guess you are using single-file-components? If so, you need to remove the template part. Vue.js can’t merge templates. So it replaces the template of the extended component. And if you have an empty template tag, it will replace the base components template where the canvas is and some other markup with an empty div.

This will not work


<template>

</template>

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

export default {
  extends: Bar,
  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>

</style>

This will work


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

export default {
  extends: Bar,
  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>

</style>
3reactions
aperturelesscommented, Jan 12, 2018

Its added to the readme

Read more comments on GitHub >

github_iconTop Results From Across the Web

vue.js - this.$refs.canvas undefined in vuejs - Stack Overflow
In my project I got undefined with this.$refs.canvas . Also when I use document.getElementById('canvas') , I get null .
Read more >
[Solved]-this.$refs.canvas undefined in vuejs-Vue.js
Coding example for the question this.$refs.canvas undefined in vuejs-Vue.js.
Read more >
[SOLVED] This.$refs.key returns undefined when it really is
I am using Vue 2.0 with Webpack. When I say console.log(this.$refs) (inside created() method inside a low level component) it retu…
Read more >
How to use React createRef - LogRocket Blog
Common errors with React refs. When working with refs in React, you may run into the problem — "TypeError: Cannot read properties of...
Read more >
Focus management with Vue refs - Learn web development
This means there's no element to bind the ref to, so it becomes undefined . You might now be thinking "hey, don't we...
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