-3

I'm trying to write a simple web component using LitElement. When I try to use:

 - @customElement('my-element')
 - @property()

I'm getting error.

Support for the experimental syntax 'decorators-legacy' isn't currently enabled.

Is this something related to babel?

import { LitElement, html } from 'lit-element';

@customElement('my-element')
class MyElement extends LitElement {
  render(){    
    return html`
      <!-- template content -->
      <p>A paragraph</p>
    `;
  }
}
Penny Liu
  • 15,447
  • 5
  • 79
  • 98
kutty
  • 1
  • 1

2 Answers2

1

JS decorators are still a proposal but they can be enabled in Babel by using @babel/plugin-proposal-decorators. Class fields also have to be enabled in order to use the @property decorator.

Install the plugins:

$ npm i -d @babel/plugin-proposal-decorators @babel/plugin-proposal-class-properties

and add them to your .babelrc:

{
  "plugins": [
    ["@babel/plugin-proposal-decorators", {
      "legacy": true
    }],
    "@babel/plugin-proposal-class-properties"
  ]
}
Umbo
  • 3,042
  • 18
  • 23
0

Decorators are a proposal for JS but are supported in Babel (as already covered by Umbo's answer, which you should accept) and natively supported by TypeScript (which Lit targets).

To use these in TS you have to add a setting to tsconfig.json:

{
  "compilerOptions": {
    "experimentalDecorators": true

And then import the decorators:

import {html, css, LitElement} from 'lit';
import {customElement, property} from 'lit/decorators.js';

@customElement('my-element')
class MyElement 
    extends LitElement {
    render(){    
        return html`<p>A paragraph</p>`;
    }
}

You should accept the Babel answer to this question, but it should be clear that this works in Typescript too.

Keith
  • 150,284
  • 78
  • 298
  • 434