2016-11-23 6 views
0

これは私のAuthServiceです。どのようにしてこのすべてのコンポーネントでこの認証サービスを使用できますか?私はこのスクリプトを実行すると以下のエラーが表示されます - プロパティ 'userLoggedIn'がタイプ 'AuthService'に存在しません。どのように私はangular2の依存性注入を使用することができます

import { Component, Input, Inject, ReflectiveInjector, Injectable} from '@angular/core'; 
@Injectable() 
export class AuthService { 
    static userLoggedIn : boolean = false; 
    //call this function when login status changes 
    static changeLoginStatus(status: boolean){ 
     this.userLoggedIn = status; 
    } 
} 

コンポーネントファイル -

import { Component, OnInit, Inject, ReflectiveInjector } from '@angular/core'; 
import { FormGroup, FormBuilder, Validators, AbstractControl, FormControl } from '@angular/forms'; 
import { Http, Response, RequestOptions, Headers } from '@angular/http'; 
import {Observable} from 'rxjs/Rx'; 
import 'rxjs/add/operator/map'; 
//import './rxjs-operators'; 

import { CustomValidators } from '../common/validations.ts'; 
import { AuthService } from '../injectables/authservice.ts'; 

@Component({ 
    selector: 'app-login', 
    templateUrl: './login.component.html', 
    styleUrls: ['./login.component.css'] 
}) 
export class LoginComponent implements OnInit { 
    loginForm : FormGroup; 
    data : any; 
    //http : Http; 

    constructor(private http: Http, fb: FormBuilder) { 
    this.loginForm = fb.group({ 
     'email':[null, Validators.compose([Validators.required,CustomValidators.emailFormat])], 
     'password':[null, Validators.compose([Validators.required])], 
    }); 
    } 

    ngOnInit() { 

    } 

    submitLoginForm(value: any){ 
     console.log(value); 
     let headers = new Headers(); 
     headers.append('Content-Type', 'application/json'); 
     let body = JSON.stringify(value); 

     this.http.post(
     'http://192.168.1.90/articles/data/post/', 
     body) 
     .subscribe((res: Response) => { 
     this.data = JSON.stringify(res); 
     console.log('---->'+res.json().data.email); 
     localStorage.setItem('email', res.json().data.email); 
     localStorage.setItem('userID', res.json().data.id); 
     localStorage.setItem('name', res.json().data.name); 
     localStorage.setItem('loginStatus', 'true'); 
     //this.loginStatus = true; 
     //aService: AuthService; 
     AuthService.changeLoginStatus(true); 
     console.log('localstorege item ---->'+localStorage.getItem('email')); 
     }); 
     return false; 
    } 

} 
+1

https://angular.io:AuthServiceを使用するすべてのコンポーネントは、そのconstructorパラメータ、例えば

constructor( private myAuthService: AuthService, // <-- here private http: Http, fb: FormBuilder ) { // ... } submitLoginForm(value: any) { // ... this.myAuthService.changeLoginStatus(true); // <-- here } 

また、あなたがたとえば、あなたのNgModuleプロバイダでAuthServiceを使用していることを確認して、それを持っている必要があります/docs/ts/latest/cookbook/component-communication.html#!#bidirectional-serviceはうまく説明しています。 質問には、達成しようとするもの、試したもの、失敗した場所を示すコードが含まれている必要があります。 –

+0

check https://angular.io/docs/ts/latest/cookbook/dependency-injection.html – ranakrunal9

+0

コードスニペットを追加しました。あなたは私に示唆してもらえますか? –

答えて

0
はあなた AuthService

export class AuthService { 
    userLoggedIn : boolean = false; 
    changeLoginStatus(status: boolean){ 
     this.userLoggedIn = status; 
    } 
} 

への依存性の注入は、あなたの constructorパラメータを介して動作変更

@NgModule({ 
    // ..... 
    providers: [ AuthService ] // <-- here 
    // ..... 
}) 
+0

お返事ありがとうございます。それでも私は同じエラーが発生しています。さて、これは私の最新のコードです。 '@ angle/core'の{Component、Input、Inject、ReflectiveInjector、Injectable}をインポートします。 @Injectable() エクスポートクラスAuthService { static userLoggedIn:boolean = false; コンストラクタ(){} //ログインステータスが変更されたときにこの関数を呼び出します。 static changeLoginStatus(status:boolean){ AuthService.userLoggedIn = status; } } このようなコンポーネントからこれを呼び出しています - AuthService.changeLoginStatus(true); –

+0

@vasanthkumar、そのサービスを使用するコンポーネントコードを投稿できますか? – Michael

+0

私のコンポーネントを投稿しました。あなたは一度それを確認できますか? –

関連する問題