0

I have a vue.js 2.0 app, and i need to publish the first apex chart example :https://apexcharts.com/docs/vue-charts/

For now, my app is not using the IMPORT syntax and working well . I'm not using Babel or WebPack.

This is my router :

const routes = [
     { path: "/home", component: Home }
];
const router = new VueRouter({
  routes // short for `routes: routes`
});

const app = new Vue({
  router
}).$mount("#app");

This is my Home component :

const Home = {
  data: function() {
    return {
      count: 0
    };
  },
  template:
    `<div>Lots of informations</div> <span>I need to place the chart there</span>`
};

If you look at the ApexChart 1st example, i have to use IMPORT and the template balise :

  1. Import doesnt work ( error) :

    SyntaxError: import declarations may only appear at top level of a module

    [Vue warn]: Unknown custom element: - did you register the component correctly? For recursive components, make sure to provide the "name" option.

  2. I can't place a template inside another template .

how could I do ?

Where can I place this code ?

<template>
<div>
  <apexchart width="500" type="bar" :options="options" :series="series"></apexchart>
</div>
</template>

I am loading Apexcharts in index.html like this :

<script src="https://cdn.jsdelivr.net/npm/apexcharts" type="module"></script>
<script src="https://cdn.jsdelivr.net/npm/vue-apexcharts" type="module"></script>

EDIT 2 :

This is my updated component :

const Home = {
  data: function() {
    return {
      options: {
        chart: {
          id: 'vuechart-example'
        },
        xaxis: {
          categories: [1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998]
        }
      },
      series: [{
        name: 'series-1',
        data: [30, 40, 45, 50, 49, 60, 70, 91]
      }]
    }
  },
  template:
    '<div><apexchart width="500" type="bar" :options="options" :series="series"></apexchart></div>'
};

I'm still getting the following errors :

**SyntaxError: import declarations may only appear at top level of a module**

[Vue warn]: Unknown custom element: <apexchart> - did you register the component correctly? For recursive components, make sure to provide the "name" option.

found in

---> <Anonymous>
       <Root>

I'm trying to import like this inside of INDEX.HTML :

<script>
import VueApexCharts from 'vue-apexcharts'
Vue.component('apexchart', VueApexCharts)
</script>

Do i have to use Babel or something, for import to work ?

3
  • What does your html look like? You should be able to put somerthing like <apex-chart></apex-chart> in your home component. Commented Oct 21, 2019 at 18:40
  • Thanks, my html are literal templates strings written inside of the JS file ... Commented Oct 21, 2019 at 18:43
  • [Vue warn]: Unknown custom element: <apexchart> - did you register the component correctly? For recursive components, make sure to provide the "name" option. Commented Oct 21, 2019 at 18:48

2 Answers 2

0

You need to import the chart component inside of your homecomponent.

import VueApexCharts from 'vue-apexcharts'
Vue.component('apexchart', VueApexCharts)

const Home = {
  data: function() {
    return {
      options: {
        chart: {
          id: 'vuechart-example'
        },
        xaxis: {
          categories: [1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998]
        }
      },
      series: [{
        name: 'series-1',
        data: [30, 40, 45, 50, 49, 60, 70, 91]
      }]
    }
  },
  template:
    '<div><apexchart width="500" type="bar" :options="options" :series="series"></apexchart></div>'
};
Sign up to request clarification or add additional context in comments.

11 Comments

Thanks, tried to copy this insert inside of template, but iim getting 2 errors :
SyntaxError: import declarations may only appear at top level of a module
[Vue warn]: Unknown custom element: <apexchart> - did you register the component correctly? For recursive components, make sure to provide the "name" option.
Post your entire HomeComponent in your question.
Move the import to your home component.
|
0

Yes, you need to use Babel and some sort of bundler (like webpack) to transpile import statements.

Vanilla JS in the browser doesn't support modules yet.

That, or the component you're using needs to have exposed a browser/CDN version that will define and bootstrap the component globally

11 Comments

How does he get Vue warnings if he isn't using a bundler?
Oh, that's a good point. The INDEX.HTML bit lead me to believe the <script> tag with the import statement was not being run through webpack (which it might not be)
OK, but this guy seems to make it work without Web pack ; medium.com/@steveolensky/…
He's using browser/CDN <script> tags to import the components. You're attempting to use module loading without webpack.
No, you need to completely remove the import statement. You do not need to import anything if you're using <script src="https://cdn.jsdelivr.net/npm/apexcharts"></script> <script src="https://cdn.jsdelivr.net/npm/vue-apexcharts"></script>
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.