[英]Can't resolve all parameters for ToastrService: (?, [object Object], [object Object], [object Object], [object Object])
I am applying toasterjs in my angular component.我正在我的角度组件中应用 toasterjs。 Here is my code.这是我的代码。 i have imported the ToastrService
using the code:我已经使用以下代码导入了ToastrService
:
import { ToastrService } from 'ngx-toastr';
and inject that service in the constructor并在构造函数中注入该服务
constructor(private toastrService: ToastrService) {
}
this.toastrService.success('You are awesome!', 'Success!',)
But it is giving the following error:但它给出了以下错误:
Can't resolve all parameters for ToastrService: (?, [object Object], [object Object], [object Object], [object Object])无法解析 ToastrService 的所有参数:(?, [object Object], [object Object], [object Object], [object Object])
When I go to its definition, then it is taking 4 parameters.当我转到它的定义时,它采用 4 个参数。 Here are the parameters:以下是参数:
success(message?: string, title?: string, override?: Partial<IndividualConfig>): ActiveToast;
I have added the module also:我还添加了模块:
imports: [
ToastrModule.forRoot(),
CommonModule,
HttpModule,
FormsModule,
ReactiveFormsModule,
TextboxModuleShared,
DateTimeModuleShared,
DropdownModuleShared
],
So how can I use the parameters because I want to only show success message?那么如何使用参数,因为我只想显示成功消息?
You have imported the ToastrModule
in your app's NgModule in the incorrect order.您以错误的顺序在应用程序的 NgModule 中导入了ToastrModule
。 It should be imported in the order as indicated by the documentation :它应该按照文档中指示的顺序导入:
@NgModule({
imports: [
CommonModule,
BrowserAnimationsModule, // required animations module
ToastrModule.forRoot(), // ToastrModule added
],
bootstrap: [App],
declarations: [App],
})
class MainModule {}
You should also replace CommonModule
with BrowserModule
if this app is running in the browser as indicated here .如果此应用程序如此处所示在浏览器中运行,您还应将CommonModule
替换为BrowserModule
。
Note that the error doesn't occur while calling the success
function, but while invoking the constructor of the ToastrService
.请注意,在调用success
函数时不会发生错误,而是在调用ToastrService
的构造函数时发生ToastrService
。 Your error message clearly states that not all parameters for the ToastrService constructor can be resolved.您的错误消息明确指出并非 ToastrService 构造函数的所有参数都可以解析。 Some of these parameters cannot be resolved, because no instances for those parameters have been created yet.其中一些参数无法解析,因为尚未创建这些参数的实例。 To understand how this works, you should read the documentation on dependency injection within the Angular framework.要了解这是如何工作的,您应该阅读有关 Angular 框架内的依赖注入的文档。
Point to Consider : I was facing same issue.需要考虑的问题:我遇到了同样的问题。 So here how I get solution所以在这里我如何获得解决方案
After installing 'ngx-toastr' Package and updating angular.json file with 'toastr.css安装“ngx-toastr”包并使用“toastr.css”更新angular.json文件后
angular should be re-run by using ng-serve command, because app is not rendered by changing angular.json file angular 应该使用 ng-serve 命令重新运行,因为应用程序不是通过更改 angular.json 文件来呈现的
尝试导入 'Inject' 装饰器,然后在构造函数中装饰参数,作为参数,您应该像这样添加 ToastrService:
constructor(@Inject(ToastrService) private toastyService: ToastrService ) { }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.