Search code examples
ecmascript-6jspm

ES6 Class gives error when I instantiate it


I am getting up to speed on ES6 classes loaded with JSPM.

For example:

export class Alerter{
    doAlert(message)
    {
        alert(message);
    }
}

Then I import this above another class:

import Alerter from 'services/alerter';

Then I use the class:

var alerter = new Alerter();

This line throws an error: object does not support his method.

Is there a different way I should be writing this?


Solution

  • You are exporting a named export and importing the default export. You either need to do

    export default class Alerter {
    

    so that you have an export to import, or

    import {Alerter} from 'services/alerter';
    

    so that you import the correct constructor.