2016-07-05 7 views
6

私はコードがどのようなものか、最新の角度2. authentication.service.ts活字体 - パラメータ「U」を暗黙的に「あらゆる」タイプ

にこのコードを変換していますか?

app/auth/auth.service.ts(30,40): error TS7006: Parameter 'u' implicitly has an 'any' type. 

// services/auth.service.ts 
import {Injectable} from '@angular/core'; 
import {Router} from '@angular/router'; 

//http://4dev.tech/2016/03/login-screen-and-authentication-with-angular2/ 
//https://github.com/leonardohjines/angular2-login 
export class User { 
    constructor(
    public email: string, 
    public password: string) { } 
} 

var users:any = [ 
    new User('[email protected]','adm9'), 
    new User('[email protected]','a23') 
]; 

@Injectable() 
export class AuthenticationService { 

    constructor(
    private _router: Router){} 

    logout() { 
    localStorage.removeItem("user"); 
    this._router.navigate(['Login']); 
    } 

    login(user:any){ 
    var authenticatedUser = users.find(u => u.email === user.email); 
    if (authenticatedUser){ 
     localStorage.setItem("user", authenticatedUser); 
     this._router.navigate(['Home']);  
     return true; 
    } 
    return false; 

    } 

    checkCredentials(){ 
    if (localStorage.getItem("user") === null){ 
     this._router.navigate(['Login']); 
    } 
    } 
} 
+0

このエラーは修正されていますか? –

答えて

9

あなたはUserタイプの代わりanyを使用するように試みることができる:

var users:User[] = [ 
    (...) 
]; 

var authenticatedUser = users.find((u:User) => u.email === user.email); 
2

は、問題の理由は、この場合のように、適切に定義されていないパラメータがあります次の例に続く変数「u」です。

var authenticatedUser = users.find(u => u.email === user.email); 

あなたがサービスのクラスモデルを持っていけない場合、あなたはその瞬間に必要なものを文字列または他のタイプとして「U」を定義する必要があります。

var authenticatedUser = users.find(u:string => u.email === user.email); 
0

私はあなたが使用したのと同じ例を使用しています。

anyを適用する代わりに、パラメータタイプをUserに設定します。

だから、あなたのログイン方法は、このようなものになるだろう:

login(user: User): boolean { ...

その後、anyキーワードへの参照を削除します。

-2

変更

tsconfig.jsonファイル

"noImplicitAny":偽、

および追加

'はNG2-formly':「NPM:NG2-formly /バンドル/ ng2- formly.umd.js '、

to systemjs.config.js

7

(u:any)=> ...

関連する問題