我如何在宗地捆扎机中使用类字段?

问题描述 投票:1回答:1

我使用公共ES6类字段声明时收到错误“ Missing class properties transform.”。如何修改我的包裹设置以允许这样做?

当前代码

index.html

<body>
  <div id="greeting"></div>
  <script src="./app.js"></script>
</body>

app.js

class Greeter {
  greeting = 'Hello';

  constructor(container, name) {
    container.innerText = `${this.greeting} ${name}`;
  }
}

new Greeter(document.querySelector('#greeting'), 'Justin');

解决方法

只有删除greeting = 'Hello';声明,我才能使它起作用:

class Greeter {

  constructor(container, name) {
    this.greeting = 'Hello';
    container.innerText = `${this.greeting} ${name}`;
  }
}

new Greeter(document.querySelector('#greeting'), 'Justin');
javascript es6-class parceljs
1个回答
1
投票

将以下内容添加到文件.babelrc

{
  "plugins": ["@babel/plugin-proposal-class-properties"]
}

有关更多详细信息,请参见@babel/plugin-proposal-class-properties。您也应该能够使用public,static和新的private fields

© www.soinside.com 2019 - 2024. All rights reserved.