如何在Angular 9中将对话框注入到服务中



我想将一个MatDialog注入到一个服务中,而不将其初始化到构造函数中。这可能吗?如果是,我该如何做到这一点?目标是将服务调用到类中,然后在该类中注入MatDialog和其他组件

这是我的实现

ClassBook {
private bookService: BookService = new BookService(); // no parameters hence the empty constructor
public createBook(book: Book){
this.bookService.addBook(book);
}
}
export class BookService{
constructor() { }

// if i just call this, the this.dialog.open will be undefined
public dialog: MatDialog;

public addBook(book){
const dialogRef = this.dialog.open(BookDialog, {
width: '500px',
disableClose: true
data: {
book
}
});
}
}

我将在AppModule中使用一个静态注入器,然后在我需要的类中作为单例调用它

最新更新