2017-02-24 82 views
0

以下是我如何在我的教程应用程序中实现警报服务,但它不起作用。请问有什么问题?登录页面和注册页面上的警报消息都没有工作角度2警报服务

登录组件

login(){ 
    this.authenticateLoginService.login(this.login.email, this.login.password) 
     .subscribe(data => { 
      this.redirectToLoginPage(); 
     }, 
     error => { 
      this.alertService.error('Invalid Username or Password'); 
      this.loading = false; 
     }); 
} 

注册页面

Register(){ 
    this.httpService.createUser(this.module) 
     .subscribe(data => { 
     this.alertService.success('Registration Successful', true); 
     this.redirectToLoginPage() 
     console.log(data); 
     }, 
     error =>{ 
     this.alertService.error(error); 
     }); 
} 

提醒服务

import { Injectable } from '@angular/core'; 
import { Router, NavigationStart } from '@angular/router'; 
import { Observable } from 'rxjs'; 
import { Subject } from 'rxjs/Subject'; 

@Injectable() 
export class AlertService { 
    private subject = new Subject<any>(); 
    private keepAfterNavigationChange = false; 

    constructor(private router: Router) { 
     // clear alert message on route change 
     router.events.subscribe(event => { 
     if (event instanceof NavigationStart) { 
      if (this.keepAfterNavigationChange) { 
       // only keep for a single location change 
       this.keepAfterNavigationChange = false; 
      } else { 
       // clear alert 
       this.subject.next(); 
      } 
     } 
    }); 
    } 

    success(message: string, keepAfterNavigationChange = false) { 
     this.keepAfterNavigationChange = keepAfterNavigationChange; 
     this.subject.next({ type: 'success', text: message }); 
    } 

    error(message: string, keepAfterNavigationChange = false) { 
     this.keepAfterNavigationChange = keepAfterNavigationChange; 
     this.subject.next({ type: 'error', text: message }); 
    } 

    getMessage(): Observable<any> { 
     return this.subject.asObservable(); 
    } 
} 

alert.ts

import { Component, OnInit } from '@angular/core'; 
import { AlertService } from '../Services/alert_services'; 

@Component({ 
    selector: 'alert', 
    templateUrl: 'alert.html' 
}) 

export class AlertComponent { 
    message: any; 

    constructor(private alertService: AlertService) { } 

    ngOnInit() { 
     this.alertService.getMessage().subscribe(message => { this.message = message; }); 
    } 
} 

alert.html

<div *ngIf="message" [ngClass]="{ 'alert': message, 'alert-success': message.type === 'success', 'alert-danger': message.type === 'error' }">{{message.text}}</div> 

回答

2

您需要在您的app.component.html添加这样 <div class="col-sm-8 col-sm-offset-2"> <alert></alert> <router-outlet></router-outlet> </div>

+0

你好akiv,我做了同样的事情,但仍然没有working.subject.next没有发射ngonint .. so警报没有更新。 – user3090790

0

我没有检查你的代码的逻辑,但一个明显的事情是,我没有看到你提供AlertService。 因此,您可以选择在AppModule中或使用该服务的每个组件中提供。例如:

  import { Component, OnInit } from '@angular/core'; 

      import { AlertService } from '../Services/alert_services'; 

      @Component({ 
       selector: 'alert', 
       templateUrl: 'alert.html', 
       providers: [AlertService], //add this line 
      }) 

      export class AlertComponent 

      { 
      message: any; 

      constructor(private alertService: AlertService) { } 

      ngOnInit() { 
       this.alertService.getMessage().subscribe(message => {   this.message = message; }); 
      } 
     } 
+0

OK,那你能不能跟我们分享的错误你得到,当你打开你的浏览器控制台? – mickdev