Angular 服务在每次调用时重置

Angular Service is reset on each call of it

在我的网络 Angular 项目中,我创建了一个 AuthenticationGuard 和一个 AuthenticationService 来处理安全问题。

这些文件来自我项目的另一个完美运行的分支。

我的脚本应该是这样工作的:

  1. 导航到 'auth/login'
  2. 用户输入他的凭据
  3. Authservice调用后端wepApi获取Bearer Token
  4. 后端return令牌。
  5. AuthService 将他的变量 'isLoggedIn' 设置为 true;
  6. AuthService 使用路由器导航到“/home”
  7. AuthGuard 通过检查 AuthService 的 'isLoggedIn' 来检查身份验证。

我的问题是当 AuthGuard 访问 AuthService 时:AuthService 总是 return false。

auth.guard.ts

import { Injectable }       from '@angular/core';
import { CanActivate, Router, ActivatedRouteSnapshot,RouterStateSnapshot } from '@angular/router';
import { AuthService }      from './auth.service';

@Injectable()
export class AuthGuard implements CanActivate {
  constructor(private authService: AuthService, private router: Router) {}

  canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {
    let url: string = state.url;

    return this.checkLogin(url);
  }

  checkLogin(url: string): boolean {
    if (this.authService.getIsLoggedIn()) { 
      return true; 
    }

    // Store the attempted URL for redirecting
    this.authService.redirectUrl = url;

    // Navigate to the login page with extras
    this.router.navigate(['/auth/login']);
    return false;
  }
}

auth.service.ts

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

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

import { config } from './../shared/smartadmin.config';

import { Http, Headers, RequestOptions, Response } from '@angular/http';
import 'rxjs/add/operator/map'

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

    public redirectUrl: string;

    constructor(private router: Router, private http: Http) {
    }

    public getIsLoggedIn(): boolean {
        console.log("getIsLoggedIn() = " + this.isLoggedIn); // Always returns false
        return this.isLoggedIn;
    }

    public login(username: string, password: string) {
        this.ProcessLogin(username, password)
            .subscribe(result => {
                if (result === true) {
                    console.log("before attribution");
                    console.log("this.isLoggedIn = " + this.isLoggedIn); // returns false
                    this.isLoggedIn = true;
                    console.log("after attribution");
                    console.log("this.isLoggedIn = " + this.isLoggedIn); // returns true
                    this.router.navigate(this.redirectUrl ? [this.redirectUrl] : ['/home']);
                } else {
                    this.logout();
                }
            });
    }


    public logout(): void {
        localStorage.removeItem('oAuthToken');
        this.isLoggedIn = false;
    }

    private ProcessLogin(username: string, password: string): Observable<boolean> {

        let headers = new Headers({ 'Content-Type': 'application/x-www-form-urlencoded' });
        let options = new RequestOptions({ headers: headers });
        let body = 'grant_type=password&username=' + encodeURIComponent(username) + '&password=' + encodeURIComponent(password);

        let endpoint = config.API_ENDPOINT + 'token';

        return this.http.post(endpoint, body, options)
            .map((response: Response) => {
                // login successful if there's a jwt token in the response
                let token = response.json() && response.json().access_token;
                if (token) {
                    localStorage.setItem('oAuthToken', token);

                    // return true to indicate successful login
                    return true;
                } else {
                    localStorage.removeItem('oAuthToken');
                    // return false to indicate failed login
                    return false;
                }
            });
    }
}

在没有看到您的模块定义的情况下,我怀疑您没有将 AuthService 设为核心服务(单例),这意味着使用它的每个模块都将拥有自己的实例(跟踪自己的 isLoggedIn 标志) .

要使服务成为 angular 中的单例,它必须由根模块注入器提供服务。为此,您需要执行以下操作:

import { NgModulep } from '@angular/core';
import { CommonModule, ModuleWithProviders } from '@angular/common';
import { AuthService, AuthGuard } from './services/index';

@NgModule({
  imports: [
    CommonModule,
    ModuleWithProviders
  ]
})
export class SharedModule {

  static forRoot(): ModuleWithProviders {
    return {
      ngModule: SharedModule,
      providers: [
        AuthService,
        AuthGuard
      ]
    };
  }

}

然后在将 SharedModule 导入根 AppModule 时调用 forRoot 方法。

@NgModule({
  imports: [
    ...
    SharedModule.forRoot(),
    ...
  ],
  ...,
  bootstrap: [AppComponent]
})
export class AppModule { }

看看 "Configure Core Services" 这里 https://angular.io/docs/ts/latest/guide/ngmodule.html#!#core-for-root

我有一个类似的问题,在我的例子中,这是由连接到按钮元素的点击事件引起的,但在 Chrome 中,每次点击按钮时都会提交整个表单,因为CHrome 的默认操作是在按钮上没有类型属性时将按钮单击视为提交。

The fix was to add the tag type="button" to the login button in the html

说明

Dependencies are singletons within the scope of an injector.

However, Angular DI is a hierarchical injection system, which means that nested injectors can create their own service instances. For more information, see Hierarchical Injectors.
Source

只需仔细检查您 provide 服务的位置。

列出您的模块并检查每个模块中的 providers 部分。
如果有多个实例 - 每个模块将提供自己的服务实例。

我遇到了同样的问题,我发现我将 AuthService 添加到 AppModule 并忘记将其从 AuthModule 中删除,因此登录页面(在 auth 模块中)有它的其他实例。