@babel/plugin-proposal-decorators
示例
Simple class decorator
JavaScript
@annotation
class MyClass {}
function annotation(target) {
target.annotated = true;
}
类装饰器(class decorator)
JavaScript
@isTestable(true)
class MyClass {}
function isTestable(value) {
return function decorator(target) {
target.isTestable = value;
};
}
Class method decorator
JavaScript
class C {
message = "hello!";
@bound
m() {
console.log(this.message);
}
}
function bound(value, { name, addInitializer }) {
addInitializer(function () {
this[name] = this[name].bind(this);
});
}
安装
- npm
- Yarn
- pnpm
npm install --save-dev @babel/plugin-proposal-decorators
yarn add --dev @babel/plugin-proposal-decorators
pnpm add --save-dev @babel/plugin-proposal-decorators