3

我正在 angular2 应用程序中使用 angular2 烤面包机。在应用程序启动时,我遇到了错误。

我的 app_module 如下:

import { NgModule }      from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule }   from '@angular/forms';
import { RouterModule } from '@angular/router';
import { HttpModule } from '@angular/http';
import { ReactiveFormsModule } from '@angular/forms';
import { ToasterModule, ToasterService} from 'angular2-toaster';
import { AppComponent }   from './app.component';

@NgModule({
    imports: [
        BrowserModule,
        FormsModule,
        AppRoutingModule,
        HttpModule,   
        ToasterModule
    ],
    declarations: [AppComponent, HomeComponent, 
        CategoryListComponent, ConsultSotiComponent,
        HeaderComponent, FooterComponent],
    providers: [CategoryListService, LeadService, 
        LookUpDetailsService, CompanyService, ConsultSotiService, ToasterService],
    bootstrap: [AppComponent]
})

export class AppModule { }

我也有引导这个模块。

我的 app.component 如下:

import { Component } from '@angular/core';
import {  OnInit } from '@angular/core';
import { TranslateService } from './translate/translate.service';
import { ToasterService} from 'angular2-toaster';
@Component({
    selector: 'mp-app',      
    providers: [ToasterService],  
    template: `<div>
            <toaster-container [toasterconfig]="config1"></toaster-container>
            <button (click)="popToast()">pop toast</button><br/>
        </div>`

})

export class AppComponent implements OnInit {
    title = 'Enterprise MarketPlace';
    public translatedText: string;
    public supportedLanguages: any[];

    constructor(private _translate: TranslateService, private toasterService: ToasterService) {
        this.popToast();
    }

    popToast() {
        this.toasterService.pop('success', 'Args Title', 'Args Body');
    }

这会导致No Toaster Containers have been initialized to receive toasts 错误。

4

2 回答 2

8

我遇到了类似的问题。我只是没有在模板中添加烤面包机容器。检查您是否在模板中正确添加了烤面包机组件。

<toaster-container [toasterconfig]="toasterconfig"></toaster-container>

更新Angular5-Toaster 5.0.0

现在有一个 5.0.0 版本,它允许在根之外包含烤面包机容器,以及多个容器,同时保证 ToasterService 的单例。这纠正了行为

于 2017-03-05T06:39:35.493 回答
0

我有同样的错误,我像这样解决了它:首先在单独的 TS 中创建组件:

import {NgModule, Component} from '@angular/core';
import {ToasterModule, ToasterService} from 'angular2-toaster';
//import {Root} from './root.component'

@NgModule({
    imports: [ToasterModule],
    declarations: [toastComponent],
    providers: [],
    bootstrap: [toastComponent]
})

@Component({
    selector: 'root',
    template: `
            <toaster-container></toaster-container>
            <button (click)="popToast()">pop toast</button>`
})

export class toastComponent {
    private toasterService: ToasterService;

    constructor(toasterService: ToasterService) {
        this.toasterService = toasterService;
    }

    popToast() {
        this.toasterService.pop('success', 'Args Title', 'Args Body');
    }
}

之后,在你的 .module 中,在声明部分中导入 toastComponent,就是这样,你可以在你的 HTML 中放入标签记住这是选择器 toastrcomponent ....你有一个错误,因为你没有初始化 toastr 组件,所以按照我的步骤,会工作

于 2017-01-25T01:17:14.850 回答