如何解决“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 举报,一经查实,本站将立刻删除。