在Angular中注入服务时出现“无法解析组件的所有参数”错误
大家好!我是Angular和TypeScript的新手,在将服务注入组件时遇到了问题。我创建了一个简单的用户数据处理服务,但尝试使用它时出现错误:
```
EXCEPTION: Can't resolve all parameters for UserListComponent: (?).
Make sure that all the parameters are decorated with Inject or have valid type annotations and that 'UserListComponent' is decorated with Injectable.
```
这是我的代码:
**user.service.ts:**
```typescript
import { Injectable } from '@angular/core';
@Injectable()
export class UserService {
private users = ['Alice', 'Bob', 'Charlie'];
getUsers(): string[] {
return this.users;
}
}
```
**user-list.component.ts:**
```typescript
import { Component } from '@angular/core';
import { UserService } from './user.service';
@Component({
selector: 'app-user-list',
template: '
- {{ user }}