我一直在使用Angular 1,現在開始學習React,但無法弄清楚兩者之間的主要區別。看起來像React是關於創建組件的,但是我們不能在Angular中使用指令來做同樣的事情嗎?我仍然處於初學者的反應階段(通過codechool學習)。Angular 1 vs React
如果有人能夠提供一個案例並告訴我如何使用角1來解決它,然後作出反應,這將是非常有益的。
以下是我迄今爲止在React中所做的工作(創建一個可以顯示Comment組件的評論的CommentBox組件)。例如,我怎麼能在角度1中做到這一點,以便我可以看到差異。
CommentBox組件:
class CommentBox extends React.Component {
render() {
const comments = this._getComments() || [];
return(
<div className="comment-box">
<h3>Comments</h3>
<div className="comment-list">
{comments}
</div>
</div>
);
}
_getComments() {
const commentList = [
{ id: 1, author: 'Clu', body: 'Just say no to love!', avatarUrl: 'images/default-avatar.png' },
{ id: 2, author: 'Anne Droid', body: 'I wanna know what love is...', avatarUrl: 'images/default-avatar.png' }
];
return commentList.map((comment) => {
return (<Comment
author={comment.author}
body={comment.body}
avatarUrl={comment.avatarUrl}
key={comment.id} />);
});
}
}
註釋組件:
class Comment extends React.Component {
render() {
return(
<div className="comment">
<img src={this.props.avatarUrl} alt={`${this.props.author}'s picture`} />
<p className="comment-header">{this.props.author}</p>
<p className="comment-body">
{this.props.body}
</p>
</div>
);
}
}
謝謝你提供的角度版本。所以看起來主要的區別在於這種虛擬dom的反應必須提供。您提供的角碼1代碼看起來更容易做出反應。 –
@ user5694093是的,它有所不同,但是Angular有更多的選擇/已知路徑來完成常用的事情。通常情況下,您不需要對角度做任何事情負責,並且框架似乎更偏向於構建默認值(主要用於使用$ http或$ resource與API進行通信以及有關數據模型和更新視圖的方式被處理)。在那裏存在權衡,更容易完成最常見的事情,但是如果你不小心創建了一個視圖來執行這個視圖,那麼這個視圖會非常難以確定框架正在爲你「做什麼」 – shaunhusain