Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A Gotcha on component creation. #92

Closed
treaves opened this issue Apr 20, 2017 · 6 comments
Closed

A Gotcha on component creation. #92

treaves opened this issue Apr 20, 2017 · 6 comments

Comments

@treaves
Copy link

treaves commented Apr 20, 2017

I am just integrating this into a project. I've created a component copy-pasted from 'How to use' section, the Bar chart. When I then used that component, it blew up with an error:

Cannot read property 'getContext' of undefined

This puzzled me. :) The issue is that in my Vue component, I had an (empty) template tag, as I just automatically do that to every new .vue file. Removing the template tag resoled the issue.

I did not see any reference to forbidding the empty template tag. I thought perhaps it'd be worth mentioning.

Thanks.

@apertureless
Copy link
Owner

Well, it makes sense if you think about it.

You are creating a component:

// CommitChart.js
import { Bar } from 'vue-chartjs'

export default Bar.extend({
  mounted () {
    // Overwriting base render method with actual data.
    this.renderChart(data, options)
  }
})

But, you are extending the base chart component. Which is for example this one: https://github.com/apertureless/vue-chartjs/blob/develop/src/BaseCharts/Bar.js

Now in your commitchart.js or in your case your commitchart.vue you have access to all data models and all props and all methods, which are defined in the base chart.

You can access the defaultOptions the chartId or width props etc. Thats why you can call the renderChart() method in your mounted() hook.

And you can create new props and data models in your commit chart component and add custom logic to it.


However, if you create a template tag in your component, Vue will take this template tag as your component template. And because you cant "extend" templates it will replace the one defined in the base chart. If you would place a h1 tag in your component template tag, you would only get a h1. Because vue can't know how to merge your template with the template in the base charts. Thats why it will replace the template.

And if you have an empty template tag in your component it will replace the whole canvas and template defined in the base chart, with... nothing ... and you lose the this.$refs.canvas

:)

@treaves
Copy link
Author

treaves commented Apr 20, 2017

Well, that 'thinking' part always gets me!

I'm new to JavaScript, and am still learning. It does make sense once one understand what you so clearly explained.

Thanks so much.

@treaves treaves closed this as completed Apr 20, 2017
@apertureless
Copy link
Owner

You're welcome :)

@dapobelieve
Copy link

Lovely explanation, just got saved from this too

@uoon-dev
Copy link

Thanks for your nice explanation :)
In this respect, can I understand vue has a little disadvantage? (disable merging template)

@apertureless
Copy link
Owner

Well its not really a disadvantage.
In nearly all cases you don't need the template part in your component.
Because if you extend it, you will get the predefined template with the canvas and all you need, to run chartjs.

In the very rare cases where you do need to change the template of the div + canvas, you still can. You just need to make sure that the ref is set correctly on the canvas element so the component still works.

And merging templates generally does not make much sense if you think about it. In which cases would you want to merge the template part of two components ?

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

No branches or pull requests

4 participants