Angular 4:使用Injector手动注入依赖项

问题描述 投票:0回答:1

我正在尝试为Angular Material Dialog手动注入MAT_DIALOG_DATA依赖项。我想在对话框内部重用我的组件,并作为嵌入在html中的常规组件。所以我不能使用构造函数注入,因为我需要有条件地注入此依赖项如果组件在对话框内。

constructor(private injector : Injector,  
                private activatedRoute : ActivatedRoute, 
                private fb : FormBuilder, 
                private userService : UserService) {

        this.resetToken = activatedRoute.snapshot.params['resetToken']; 
        if(this.hasResetToken()) { 
             this.dialogRef  = this.injector.get(MatDialogRef);
             this.data = this.injector.get(MAT_DIALOG_DATA); 
        }

上面的MatDialogRef工作正常,但MAT_DIALOG_DATA InjectionToken不起作用。

angular typescript dependency-injection angular5
1个回答
0
投票

这种注射工作正常。我在与未定义变量相关的hasResetToken()检查中有错误。

© www.soinside.com 2019 - 2024. All rights reserved.