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 ?

harmonius cool
  • 337
  • 1
  • 2
  • 23

2 Answers2

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>'
};
Evan Lalo
  • 1,209
  • 1
  • 14
  • 34
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

gabriel.hayes
  • 2,267
  • 12
  • 15
  • How does he get Vue warnings if he isn't using a bundler? – Evan Lalo Oct 21 '19 at 19:23
  • Oh, that's a good point. The `INDEX.HTML` bit lead me to believe the ` – gabriel.hayes Oct 21 '19 at 19:25
  • OK, but this guy seems to make it work without Web pack ; https://medium.com/@steveolensky/create-a-spa-vuejs-application-without-node-webpack-or-any-other-build-process-using-vue-router-b9bf8e36e958 – harmonius cool Oct 21 '19 at 19:34
  • He's using browser/CDN ` – gabriel.hayes Oct 21 '19 at 19:37
  • e.g., he uses a `` component; this only works because he loaded it via this `` – gabriel.hayes Oct 21 '19 at 19:38
  • Right now it looks like you're doing both. Just remove the import statement and apex charts should work fine without bundling. – gabriel.hayes Oct 21 '19 at 19:39
  • Added type="module" to the – harmonius cool Oct 21 '19 at 19:46
  • Well, what happens when you completely remove that ` – gabriel.hayes Oct 21 '19 at 19:51
  • Yes i've mooved it after new Vue(...) but it is still not working. – harmonius cool Oct 21 '19 at 19:54
  • 1
    No, you need to completely remove the `import` statement. You do not need to `import` anything if you're using ` ` – gabriel.hayes Oct 21 '19 at 19:59
  • Ah ok, thanks a lot thats what i was wondering ... but I still leave Vue.component('apexchart', VueApexCharts); Vue.use(apexchart); and it doesnt work either . If I remove all, this is the following : [Vue warn]: Unknown custom element: - did you register the component correctly? – harmonius cool Oct 21 '19 at 21:44