这是我的服务:
@Injectable({
providedIn: 'root'
})
export class PostService {
name:string;
constructor(private http: HttpClient,
private location: Location,
private router: Router
) { }
public openPostItDialog(dialog: MatDialog): void {
const dialogRef = dialog.open(PostItDialog, {
width: '250px',
data: { name: this.name }
});
const url = this.router.createUrlTree(['post']).toString();
this.location.go(url);
console.log("Dialog was open");
dialogRef.afterClosed().subscribe(result => {
console.log("The dialog was closed");
this.name = result;
this.location.go("");
})
}
// Async Calls
public fetchPosts(): Observable<Post[]> {
return this.http.get<Post[]>('api/v1/posts');
}
public createNewPost(form: FormGroup){
console.log(form);
}
}
它在app.module中提供</ p>
在此组件的构造函数中,无法解析PostService吗?
export class PostItDialog implements OnInit {
public postForm: FormGroup;
constructor(
public dialogRef: MatDialogRef<PostItDialog>,
@Inject(MAT_DIALOG_DATA) public data: PostItDialogData,
private postService: PostService
) { }
ngOnInit() {
this.postForm = new FormGroup({
'feed': new FormGroup({
'title': new FormControl(null, [Validators.required, Validators.maxLength(100), Validators.minLength(4)]),
'description': new FormControl(null, Validators.required)
}),
'movie': new FormGroup({
'title': new FormControl(null, [Validators.required, Validators.maxLength(50), Validators.minLength(3)]),
})
})
}
onSubmit(){
console.log(this.postForm);
this.postForm.reset();
this.postService.createNewPost(this.postForm)
}
onNoClick(): void{
this.dialogRef.close();
this.postForm.reset();
}
}
我不知道为什么。在其他地方注入相同的服务也很好。我可以从构造函数中删除PostService对象,并且效果很好。它只是无法解析PostService的该组件PostItDialog。知道为什么会这样吗?