我正在扩展基类并覆盖基类中的方法。但是当我调用它时,它称为超类版本。如何覆盖该方法?
var Hello = React.createClass( {
getName: function() { return "super" },
render: function() {
return <div>This is: {this.getName()}</div>;
}
});
class HelloChild extends Hello {
constructor(props) {
super(props);
console.log( this.getName());
}
getName()
{
return "Child";
}
};
我希望它打印“这是:孩子”,但它打印“这是:超级”
我找到了答案(从这里改编:https : //gist.github.com/Zodiase/af44115098b20d69c531)-基类也需要以ES6方式定义:
class Hello extends React.Component {
//abstract getName()
getName()
{
if (new.target === Hello) {
throw new TypeError("method not implemented");
}
}
render() {
return <div>This is: {this.getName()}</div>;
}
};
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句