dependency-injection - 如何在 Angular 9 中将对话框注入到服务中
问题描述
我想将 aMatDialog
注入服务而不将其初始化到构造函数中。可能吗 ?如果是,我怎么能做到这一点?目标是将服务调用到一个类中,稍后我将不得不在该类中注入 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 中使用静态注入器,然后在我需要的类中将其称为单例
推荐阅读
- java - 如果它是 WebApplicationException 的子类,我如何验证 HTTP 状态代码?
- c++ - 为什么“单一范围提案”为每个视图包含两个名称是否有特定原因?
- javascript - 如何阻止 v-hover 在 Vuetify 中触发我的函数
- reactjs - 在反应本机的持有模式上显示模式
- javascript - 添加到数据库时使用 AJAX 防止刷新
- javascript - 在从 observable 接收到特定消息后停止接收消息
- database - 人为创建 Cassandra 错误以测试重试策略
- c++ - 带有“-”前缀的命令行参数
- r - 带有 ggplot() 函数的条形图在 R-studio 中不起作用
- java - ListView CustomAdapter 位置卡在 0