Typescript-参数';u';隐式具有';任何';类型
我将此代码转换为最新的Angular 2 代码应该是什么样子Typescript-参数';u';隐式具有';任何';类型,typescript,angular,Typescript,Angular,我将此代码转换为最新的Angular 2 代码应该是什么样子 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.te
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']);
}
}
}
您可以尝试使用
User
类型而不是any
:
var users:User[] = [
(...)
];
及
问题的原因是一个未正确定义的参数,因为本例中的变量“u”如下所示:
var authenticatedUser = users.find(u => u.email === user.email);
如果您没有服务的类模型,则应将“u”定义为字符串或您当前需要的其他类型:
var authenticatedUser = users.find(u:string => u.email === user.email);
我用的是你用过的同一个例子 将参数类型设置为
User
,而不是apply any
因此,您的登录方法如下所示:
登录(用户:用户):布尔值{…
然后,删除对任何关键字的任何引用。2要进行的更改:
- 在
文件中:tsconfig.json
“noImplicitAny”:false,
- 在
中,添加:systemjs.config.js
'ng2-formly':'npm:ng2-formly/bundles/ng2-formly.umd.js',
“noImplicitAny”:false
刚刚救了我一命!
var authenticatedUser = users.find(u:string => u.email === user.email);