我正在尝试将提供程序(ChatNotificationServiceProvider
(inject
到我的AuthService
(另一个提供程序(中,但是当我将聊天通知服务提供程序inject
到我的AuthService提供程序(通过构造函数(时,出现了错误">无法解析聊天通知服务提供程序的所有参数:(?, ?("。
我读过其他解决方案,他们都说把@Injectable
放在ChatNotificationServiceProvider
里,它应该可以工作。但是,我已经输入了@Injectable
但它仍然不起作用。
这是我的代码: ChatNotificationServiceProvider.ts
import { Injectable, OnInit } from '@angular/core';
import { AuthService } from '../auth-service/auth-service';
@Injectable()
export class ChatNotificationServiceProvider {
public chat;
constructor(private authService: AuthService) {
}
AuthService.ts
import { Injectable, Injector } from '@angular/core';
import 'firebase/auth'
import * as firebase from 'firebase/app';
import 'firebase/storage';
import { ChatNotificationServiceProvider } from '../chat-notification-service/chat-notification-service';
@Injectable()
export class AuthService {
constructor(public listingProvider: ListingProvider, public chatNotificationSvc: ChatNotificationServiceProvider) <-- After injecting the ChatNotificationServiceProvider, the error mentioned above is thrown to me.
{
this.chatNotificationSvc.str = "Test string";
}
一些附加信息:我已经使用相同的方法在我的app.component中注入了ChatNotificationServiceProvider,它工作正常。不知道为什么我不能在其他提供程序中注入相同的提供程序。
请帮忙!是否可以在另一个提供程序中注入提供程序?我对此很迷茫。
编辑:这是我的提供程序在我的app.module.ts中,应@Serena的要求。
providers: [
StatusBar,
SplashScreen,
{provide: ErrorHandler, useClass: IonicErrorHandler},
FirestoreServiceProvider,
AngularFirestore,
PayPal,
ChatNotificationServiceProvider,
Storage,
Camera,
AuthService,
BookingService,
ListingProvider,
]
请删除
import { AuthService } from '../auth-service/auth-service';
来自聊天通知服务提供商
另外请检查以下内容
创建提供程序 first.provider.ts
import { Injectable } from '@angular/core';
@Injectable()
export class FirstProvider {
constructor() {
}
}
second.provider.ts
import { Injectable } from '@angular/core';
@Injectable()
export class SecondPrivider{
constructor() {
}
}
索引.ts
export { FirstProvider} from './providers/first.provider';
export { SecondProvider} from './providers/second.provider';
在应用程序中。 请添加上述提供商
providers: [
FirstProvider,
SecondProvider
]
创建服务
import { Injectable } from '@angular/core';
import { FirstProvider, SecondProvider } from './providers';
@Injectable()
export class AuthService {
constructor(private firstProvider: FirstProvider,
private secondProvider : SecondProvider ) { }
onChangeFormat(str) {
// add logic here
// this.firstProvider()
}
}