Typescript – 参数’u’隐式具有’any’类型
发布时间:2020-12-17 08:20:58 所属栏目:安全 来源:网络整理
导读:我将此代码转换为最新的Angular 2. authentication.service.ts 代码应该是什么样的? app/auth/auth.service.ts(30,40): error TS7006: Parameter 'u' implicitly has an 'any' type.// services/auth.service.tsimport {Injectable} from '@angular/core';i
我将此代码转换为最新的Angular 2.
authentication.service.ts
代码应该是什么样的? 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('admin@admin.com','adm9'),new User('user1@gmail.com','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']); } } }
您可以尝试使用用户类型而不是任何:
var users:User[] = [ (...) ]; 和 var authenticatedUser = users.find((u:User) => u.email === user.email); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |