“Null Injector Error: no provider”,前面有硬编码字符串,但不是变量

问题描述

我创建了一个服务,将来自我的 Node.js 服务器的响应消息中继到我的主要 appComponent 上的 SnackBar 通知,由我的注册 组件通过主题订阅中的嵌套行为主题订阅(BehaviorSubject 订阅会立即调用,而不是等待,因此取消了调用服务时激活的预期效果)。

我花了两天时间不停地工作以达到这一点,现在当我将硬编码传递给 signupComponent 时,整个事情工作完美无缺:notify (String) 即:

behaviorService.updateData('this_is_hardcoded'))

但是当我从我的局部变量传递我的字符串的值时不起作用(this.finalResponse).

(授权模块)signupComponent:

    onSignup(form: NgForm) {
        if (form.invalid) {
          return;
        }
        this.isLoading = true;
        this.authService.createuser(form.value.email,form.value.password).subscribe(response=>{
          this.message=JSON.stringify(response);
          this.parsed=JSON.parse(this.message);
          this.final=this.parsed.message;
      });
      this.notify(this.final);

      }
      notify(input){
     
        this.snack.updateDataSelection(**'hardcoded_works'**);
        this.snack.notification$.next();
      }

    ```

snack.service.ts

        @Injectable({ providedIn: "root" })
        export class SnackService{
            public deea:msgOb;
            public becaz:string;
            
            public notificationSource$: BehaviorSubject<string> = new BehaviorSubject('');
            

            public notification$: Subject<string> = new Subject();
          constructor(){}
          updateDataSelection(data: string){
              this.notificationSource$.next(data);
          }
        ```

**app.component**
      constructor(
        private authService: AuthService,private snack:SnackService,private snack2:MatSnackBar,// private errorService: ErrorService
      ) {
        
        this.snack.notification$.subscribe(message => {

          this.snack.notificationSource$.subscribe(message => {
            const rnus=new injectIt(message);
            this.works=rnus.getit();
          });
          
          
          
         this.showComponent(this.works);
        });

      }
      showComponent(message){

        this.snack2.openFromComponent(SuccessComponent,{
          data: message,duration: 10000
        });
      }

    ```

和我的 SnackBar 组件:“SuccessComponent”(表示响应成功)



        @Component({
            selector: 'snack-bar-component-example-snack',templateUrl: 'snack-bar-success.html',styles: [`
      .example-pizza-party {
        color: hotpink;
      }
    `],})
          export class SuccessComponent implements OnInit {
            constructor(
              public sbRef: MatSnackBarRef<SuccessComponent>,@Inject(MAT_SNACK_BAR_DATA) public data:string
            ) {}
            ngOnInit() {
                //this.data=SnackService
            }
          
          }
        ```
I also have 
     providers:[SnackService]

在我的 app.component 以及我的 app.module

当我在注册中的 onSubmit 通知中的 updateDataSelection 中使用 this.final 而不是 'hardcoded_string' 时得到的确切错误是:

SuccessComponent_Host.ngfactory.js? [sm]:1 ERROR NullInjectorError: StaticInjectorError(AppModule)[SuccessComponent -> InjectionToken MatSnackBarData]: 
  StaticInjectorError(Platform: core)[SuccessComponent -> InjectionToken MatSnackBarData]: 
    NullInjectorError: No provider for InjectionToken MatSnackBarData!
    at NullInjector.push../node_modules/@angular/core/fesm5/core.js.NullInjector.get 

此外,我一直在我的 signupScreen 视图中显示 this.final 的内容,以确保字符串非常明确并且确实是一个“字符串”。

为什么我会因为字符串作为变量而不是作为 signupComponent 中的硬编码字符串传递的区别而收到此错误

observables 和服务都运行良好,唯一的区别是注册组件中初始输入的格式。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...