【发布时间】:2016-12-05 23:49:56
【问题描述】:
我正在尝试了解装饰器如何与 Meteor 1.4 一起使用。据我read,这个功能是支持的。
现在,我不确定如何实际实施它。从this blog,装饰一个类,我需要这段代码
export const TestDecorator = (target) => {
let _componentWillMount = target.componentWillMount;
target.componentWillMount = function () {
console.log("*** COMPONENT WILL MOUNT");
_componentWillMount.call(this, ...arguments);
}
return target;
}
然后将其用作
import React, { Component } from 'react';
import { TestDecorator } from 'path/to/decorator.js';
@TestDecorator
export default class FooWidget extends Component {
//...
}
代码编译,但在渲染组件时没有任何输出。
我错过了什么?如何在 Meteor 中实现装饰器?这是正确的解决方案吗?有什么选择?
编辑
我试过了,还是不行
export const TestDecorator = (target) => {
console.log("*** THIS IS NOT EVEN DISPLAYED! ***");
target.prototype.componentWillMount = function () {
// ...
};
}
【问题讨论】:
-
装饰器是一个提议,它们不是 ES7 的一部分。
-
很公平。谢谢。
标签: meteor reactjs decorator babeljs ecmascript-next