TypeScript中的Property has no initializer错误:如何修复?
大家好!我是TypeScript和Angular的新手,遇到了一个非常奇怪的错误。我有一个组件,在其中声明了一个属性,但TypeScript报错说它没有初始化。这是我的代码:
```typescript
import { Component, Input } from '@angular/core';
@Component({
selector: 'app-user',
template: `
`
})
export class UserComponent {
@Input() user: User;
public userName: string;
private id: number;
constructor() {
// 在构造函数中什么都不做
}
ngOnInit() {
this.userName = this.user.name;
this.id = this.user.id;
}
}
```
编译时我收到以下错误:
- `Property 'user' has no initializer and is not definitely assigned in the constructor.`
- `Property 'userName' has no initializer and is not definitely assigned in the constructor.`
- `Property 'id' has no initializer and is not definitely assigned in th
{{ user.name }}
{{ user.email }}