1

我已经通过使用此链接在我的 Angular 4 项目中实现了烤面包机 - > http://www.codershood.info/2017/04/14/showing-notification-using-toaster-angular-2/

当我通过单击按钮触发它时,它工作得非常好

我添加了一个按钮,例如

<div class="btn btn-success" (click)="showSuccess()">Success Notification</div>

我添加了以下功能

public showSuccess(){    
      this.toastr.success("Success", 'You are on right track.');
  }

它工作得非常好,但是当我尝试像以下方式调用它时,它就不起作用了:

编辑:添加了完整的组件代码

import { Component, OnInit,ViewContainerRef } from '@angular/core';
import { FormsModule }   from '@angular/forms';
import {Router} from '@angular/router';
import { AuthService } from '../services/auth.service';
import { Constants } from '../../constants';
import { ToastsManager } from 'ng2-toastr/ng2-toastr';
import { EmployeeComponent } from '../employee/employee.component';





@Component({
  selector: 'app-login',
  templateUrl: './login.component.html',
  styleUrls: ['./login.component.css']
})

export class LoginComponent implements OnInit {

     title = 'GGMD ';
   invalidLogin: boolean;

  constructor(
    private router: Router,
    private authService: AuthService,
    public toastr: ToastsManager, 
    public _vcr: ViewContainerRef) {
    this.toastr.setRootViewContainerRef(_vcr);
  } 

  ngOnInit() {
  }
public showSuccess(){    
      this.toastr.success("Success", 'You are on right track.');
  }

    signIn(credentials){

        let response = this.authService.logIn(credentials);
        if(response.AuthenticationStatus === true){

          switch(response.Role){

               case 'employee':
                this.router.navigate(['/employee']);
                this.toastr.success("Success", 'You are on right track.');
                console.log("Success", 'You are on right track.');
                break;

              case 'manager':
                this.router.navigate(['/manager']);
                console.log('I am manager');
                break;

              default:
                console.log( error);
        }
      }
    }

日志正在工作并成功导航,但烤面包机不工作。我也试过像传递函数,例如

 this.router.navigate(['/employee']);
                this.showSuccess()
                console.log("Success", 'You are on right track.');
4

1 回答 1

0

这将不起作用,因为您已更改根目录this.router.navigate(['/employee']);

尝试将吐司代码放在this.router.navigate(['/employee']);类似的东西之前

      this.toastr.success("Success", 'You are on right track.');
      this.router.navigate(['/employee']);

我不确定上面是否可行其他解决方案是您可以在员工组件构造函数中显示吐司

于 2017-11-08T07:48:50.247 回答