最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

angular - JavaScript ECMAScript 6 - ERROR: "You can only use decorators on an export when exporting a class" -

programmeradmin1浏览0评论

I am trying to export a function in ECMAScript 6 so I can import it and use it in other files to have DRY code.

However, I receive the following error:

You can only use decorators on an export when exporting a class (16:0) while parsing file:

@idempotent
export function totalItems() {
    this.cart.items.forEach((dish) => total += item.qty);
    return total;
}

How can I fix this problem?

I am trying to export a function in ECMAScript 6 so I can import it and use it in other files to have DRY code.

However, I receive the following error:

You can only use decorators on an export when exporting a class (16:0) while parsing file:

@idempotent
export function totalItems() {
    this.cart.items.forEach((dish) => total += item.qty);
    return total;
}

How can I fix this problem?

Share Improve this question edited Aug 21, 2020 at 16:13 Peter Mortensen 31.6k22 gold badges110 silver badges133 bronze badges asked Jun 16, 2016 at 23:32 Hamza L.Hamza L. 1,8234 gold badges27 silver badges47 bronze badges 11
  • Are you using Babel? If so, please specify and add the appropriate tag. – Jeremy Banks Commented Jun 16, 2016 at 23:35
  • Using Ionic2, Doesn't matter as Babel just only plies the code.. – Hamza L. Commented Jun 16, 2016 at 23:36
  • Isn't this error ing from the piler? It looks like it's defined in Babel's source code. Do the lines above this define a decorator, which you need to include? It's easier to help if you provide a minimal, plete, verifiable example. Please do so in the future. – Jeremy Banks Commented Jun 16, 2016 at 23:38
  • 3 Decorators are not ES6. They are a proposal for future versions of ECMAScript, which some pilers have chosen to implement in inconsistent non-standardized ways (making your choice of piler extremely relevant). Per the proposal, they are only going to be supported for classes, not standalone functions. That's your problem. But this post doesn't even include the code responsible for the error (no decorators are visible). This post will need to go on hold until/unless it can be made plete. – Jeremy Banks Commented Jun 16, 2016 at 23:42
  • 1 what's in the line before this export. Are you actually trying to decorate that / a function? – Thomas Commented Jun 16, 2016 at 23:57
 |  Show 6 more ments

2 Answers 2

Reset to default 7

What you're trying to do is not supported. The decorator proposal (which is not yet standardized) only includes @decorators for classes definitions and their methods, not for function definitions.

You may want to create a function decorator/wrapper function to be used like this instead:

export const totalItems = idempotent(function() {
    let total;
    this.cart.items.forEach((dish) => total += item.qty);
    return total;
});

If you want to export a function then you must add into the specific class like this:

export class GeneralFunctions
{
     totalItems() {
         this.cart.items.forEach((dish) => total += item.qty);
         return total;
     }
}

And at the place you want to use that function, simply import that class and create an object of it and call that function with the object like this:

import {GeneralFunctions} from "./<your-file>";

var obj : GeneralFunctions 
obj.totalItems();

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论