2016-10-13 20 views
5

Tôi đang phát triển dự án nhỏ để thử nghiệm với góc 2 và tôi đã phản đối lỗi đã hủy đăng ký khi đăng nhậpgóc 2 đối tượng đã hủy đăng ký lỗi

Đây là LoginComponent tôi:

import {Component, OnDestroy} from '@angular/core'; 
import {Router} from '@angular/router'; 
import { Subscription } from 'rxjs/Subscription'; 
import {Location} from '@angular/common'; 


import {AuthService} from './services/auth.service'; 
import {RoutingService} from './services/routing.service'; 

import {ToastyService, ToastyConfig, ToastOptions, ToastData} from 'ng2-toasty'; 

import {LoadingBarModule, LoadingBarService} from 'ng2-loading-bar'; 


@Component({ 
    selector: 'login', 
    template: ` 
       <loading-bar color="#FF0000" [height]="3" [animationTime]="0.3" [runInterval]="100" [progress]="0"></loading-bar> 
       <h3> {{'LOGIN' | translate }} </h3> 
       <p> {{Message}} </p> 

       <div *ngIf="!authService.isLoggedIn"> 
        <input [(ngModel)]="username" placeholder="{{'USERNAME' | translate}}" /><br /> 
        <input type="password" [(ngModel)]="password" placeholder="{{'PASSWORD' | translate}}" /> 
       </div> 
       <div> 
        <button (click)="login()" *ngIf="!authService.isLoggedIn">{{'LOGIN' | translate }}</button> 
       </div> 

       <label class="label label-danger">{{errorMessage}}</label> 

       <ng2-toasty [position]="'top-center'"></ng2-toasty> 
       ` 
}) 

export class LoginComponent implements OnDestroy { 
    username: string; 
    password: string; 
    subscription: Subscription; 

    constructor(private authService: AuthService, private router: Router, private toastyService: ToastyService, private toastyConfig: ToastyConfig, private loadingBarService: LoadingBarService, private routingService: RoutingService, private location:Location) { 
     this.toastyConfig.theme = 'material'; 
    } 

    login() { 

     this.loadingBarService.start(); 

     this.subscription = this.authService.login(this.username, this.password).subscribe(() => { 

      if (this.authService.isLoggedIn) { 
       this.toastyService.default('Hi'); 

       this.routingService.logged = false; 

       let redirect = this.authService.redirectUrl ? this.authService.redirectUrl : this.routingService.lang + '/apphomecomponent'; 

       this.router.navigate([redirect]); 
      } 
      else { 
       this.toastyService.default('Login failed'); 
      } 
     }); 
    } 

    ngOnDestroy() { 
     this.subscription.unsubscribe(); 
    } 
} 

Và đây là AuthService của tôi:

import {Injectable} from '@angular/core'; 

import {Observable} from 'rxjs/Observable'; 
import 'rxjs/add/observable/of'; 
import 'rxjs/add/operator/do'; 
import 'rxjs/add/operator/delay'; 



@Injectable() 
export class AuthService { 
    isLoggedIn: boolean = false; 

    redirectUrl: string; 

    login(username: string, password: string): Observable<boolean> { 
     if (username === 'test' && password === 'test') { 
      return Observable.of(true).delay(1000).do(val => this.isLoggedIn = true); 
     } 
     else { 
      return Observable.of(false).delay(1000).do(val => this.isLoggedIn = false); 
     } 
    } 

    logout(): void { 
     this.isLoggedIn = false; 
    } 
} 

Khi tôi đăng nhập lần đầu tiên, nó hoạt động tốt. nhưng khi tôi đăng xuất và cố đăng nhập lại, nó cho tôi lỗi: enter image description here

Trả lời

1

Tôi đã gặp sự cố tương tự và "giải quyết" bằng cách thay thế * ngNếu bằng [ẩn].

Giải pháp này không xóa phần tử khỏi phần tử dom, có thể là nguyên nhân gây ra sự cố hủy đăng ký, nó chỉ ẩn phần tử.

+0

Cảm ơn bạn đã trả lời. Nhưng tôi muốn tránh sử dụng [ẩn]. Bởi vì khi bạn sử dụng [ẩn], quan trọng, Góc điều khiển kiểu này và đăng nó bằng "! Quan trọng" để đảm bảo nó luôn ghi đè bất kỳ kiểu hiển thị nào khác được đặt trên phần tử đó. Nguồn: [http://angularjs.blogspot.com/2016/04/5-rookie-mistakes-to-avoid-with-angular.html](http://angularjs.blogspot.com/2016/04/5- rookie-mistake-to-avoid-with-angular.html) –

Các vấn đề liên quan