这个绑定在Angular vs React中



我刚刚开始React,我得到了为什么我们要绑定这个的概念在React中,但是Angular是如何照顾这个我很困惑

<标题>
class Product extends React.Component {
a= "mdb"
constructor(props) {
super(props);
// this.handleUpVote = this.handleUpVote.bind(this)
}
// Inside `Product`
handleUpVote() {
console.log("we are looking at This",this.a) //undefinded
this.props.onVote(this.props.id);
}}
<标题>角h1><如何strong>/strong>没有Es7的.bind(this) or Arrow Function,我错过了什么?

这与角度或反应无关。这只是这个的行为javascript中需要明确绑定或者使用箭头函数使用词法这个绑定自动将它们绑定到定义它们的作用域。而且TypeScript非常擅长找到"最近的对象"或知道词法作用域,所以TypeScript可以给你准确的信息,告诉你应该从中得到什么。这里解释得很好,或者这里理解这个行为在javascript和react中

最新更新