简体   繁体   English

在 MatDialog 中提供额外的提供者

[英]Provide additional provider in MatDialog

Imagine you have Angular service without provideIn, so it is available only in certain components and their children.想象一下,您有没有提供的 Angular 服务,因此它仅在某些组件及其子组件中可用。 Now one such component opens MatDialog -- is it possible to have that service injected there?现在这样一个组件打开了 MatDialog —— 是否可以在那里注入该服务?

As I see by default Angular does not count MatDialog component as a child of caller component and also I do not see any settings to change that.正如我在默认情况下看到的那样,Angular 不会将 MatDialog 组件视为调用者组件的子组件,而且我也看不到任何设置来更改它。

PS Service here is stateful, so just putting it to providers of MyDialogComponent will create new service, which is not desired.这里的 PS 服务是有状态的,所以只要把它放到 MyDialogComponent 的提供者那里就会创建新的服务,这是不希望的。

All you have to do is provide the dialog.open method with ViewContainerRef where the desired service instance lives.您所要做的就是为 dialog.open 方法提供 ViewContainerRef 所需的服务实例所在的位置。 'VERY_IMPORTANT_SERVICE' will be available for injection in 'SomeComponent'. 'VERY_IMPORTANT_SERVICE' 将可用于在 'SomeComponent' 中注入。 See the code below请参阅下面的代码

@Component({
    selector: 'cmp',
    providers: [ VERY_IMPORTANT_SERVICE ]
})
export class Component {
    constructor(private dialog: MatDialog, private viewContainerRef: ViewContainerRef) {}

    openDialog() {
       this.dialog.open(SomeComponent, {
           viewContainerRef: this.viewContainerRef
       })
    }
}

If you do not provide your service into the root, then you have to provide it either in如果您不将服务提供到根目录中,那么您必须在

  • An Angular feature (component, directive...) Angular 功能(组件、指令...)
  • An Angular module一个Angular模块

Providing it into a feature implies that everytime that feature gets created, it gets an instance of that service.将其提供到功能中意味着每次创建该功能时,都会获得该服务的一个实例。

If you provide it in the module, it implies that everytime the module is created, one instance of that service is created, which will be common for every feature in that module.如果您在模块中提供它,则意味着每次创建模块时,都会创建该服务的一个实例,这对于该模块中的每个功能都是通用的。

If you wish to provide the module instance to your dialog component, you can.如果您希望将模块实例提供给您的对话框组件,您可以。 If you choose the feature providing, then you will have a new instance of that service into your dialog component (which will be useless to keep a state from your "parent" component).如果您选择提供的功能,那么您将在对话组件中拥有该服务的新实例(这对于从“父”组件中保留 state 将毫无用处)。

I hope it answers the question, and if not, feel free to ask for more explanation.我希望它回答了这个问题,如果没有,请随时要求更多解释。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何为 MatDialog 提供服务 - How to provide service to MatDialog Angular NullInjectorError:没有 MatDialog 提供程序 - Angular NullInjectorError: No Provider for MatDialog 角材料 没有 MatDialog 的提供者! 错误 - angular material No provider for MatDialog! Error 角材料StaticInjectorError:没有MatDialog的提供者 - Angular material StaticInjectorError: No provider for MatDialog NullInjectorError: 没有 MatDialog 的提供者 - 试图为 MatDialog 创建包装器服务 - NullInjectorError: No provider for MatDialog - trying to create a wrapper service for MatDialog NullInjectorError:添加mat对话框时没有提供MatDialog的提供程序 - NullInjectorError: No provider for MatDialog getting while adding mat dialog NullInjectorError:没有 MatDialog 的提供者! 使用带有垫子对话框的防护时显示错误 - NullInjectorError: No provider for MatDialog! error showing while using guard with mat dialog 错误 NullInjectorError: R3InjectorError(n)[_0 -> _0 -> _0]: NullInjectorError: No provider for _0! --- 使用 Angular MatDialog 时 - ERROR NullInjectorError: R3InjectorError(n)[_0 -> _0 -> _0]: NullInjectorError: No provider for _0! --- When using Angular MatDialog MatDialog不显示 - MatDialog not showing MatDialog 子关闭父 MatDialog - MatDialog Child Closes Parent MatDialog
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM