Angular2检测其余Api

时间:2016-09-21 11:00:41

标签: angular angular2-services

我使用简单的身份验证服务所有数据存储mysql我想检测mysql上的更改并获取更改放模型我尝试ngDoCheck生命周期但ngDoCheck每1秒发送一次api调用并崩溃apache

User.Service

import { Injectable } from '@angular/core';
import { Http, Headers, RequestOptions, Response } from '@angular/http';
import { Observable } from 'rxjs/Observable';

import { AuthenticationService } from './authentication.service';
import { User } from '../_models/user';

@Injectable()
export class UserService {
    private token: string;

    constructor(
        private http: Http,
        private authenticationService: AuthenticationService) {
    }

    getUsers(): Observable<User[]> {
        // Authorization Tokeni Ayarlanıyor
        let headers = new Headers({ 'Authorization': this.authenticationService.token });
        let options = new RequestOptions({ headers: headers });
        // Kullanıcı Headeri Gönderiliyor
        return this.http.get('http://localhost/Hesap/Detay', options)
            .map((response: Response) => response.json().detay); 
    }
}

Navbar Construct用于用户服务检查用户详细信息

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

import { AuthenticationService } from '../../_services/authentication.service';
import { User } from '../../_models/user';
import { UserService } from '../../_services/user.service';

import { Router } from '@angular/router';


@Component({
    selector: 'as-navbar',
    templateUrl: './navbar.html',
	styleUrls: ['navbar.component.css'],
})
export class NavbarComponent implements OnInit {
	private isLoggedIn = false;
	users: User[] = [];
	

	constructor(private authenticationService: AuthenticationService, private router: Router, private userService: UserService){}
	
	ngOnInit() {
		if (localStorage.getItem('currentUser')) {
			this.userService.getUsers()
            .subscribe(users => {
                this.users = users;
            });
            this.isLoggedIn = true;
       }else{
        	this.isLoggedIn = false;
        }
	}

	logOut(){
		this.users = [];
		this.authenticationService.logout();
		this.router.navigate(['/']);
	}
}

0 个答案:

没有答案
相关问题