TypeScript还是ES6:Angular新手该如何选择?
大家好!我是一名前端开发新手,正在学习Angular。我看到所有Angular项目都是用TypeScript编写的,但我已经对ES6(ES2015)相当熟悉了。我觉得ES6已经提供了箭头函数、类、模块、Promise——似乎现代开发所需的一切都有了。
例如,这是一个用ES6编写的简单组件:
```javascript
class UserComponent {
constructor(userService) {
this.userService = userService;
}
getUser(id) {
return this.userService.fetch(id).then(user => {
this.user = user;
this.render();
});
}
render() {
console.log(`User: ${this.user.name}`);
}
}
```
同样的代码用TypeScript写看起来更冗长:
```typescript
class UserComponent {
private user: any;
constructor(private userService: UserService) {}
getUser(id: number): void {
this.userService.fetch(id).then(user => {
this.user = user;
this.render();
});
}
}