angular 2 中的 TSLint 错误
TSLint mistakes in angular 2
我的代码有几个错误。我正在使用 Angular 2 + TSLint:
constructor(http: Http) {
this.http = http;
--> let currentUser = JSON.parse(localStorage.getItem("currentUser"));
this.token = currentUser && currentUser.token;
}
在当前用户中我有这个错误:message: 'expected variable-declaration: 'currentUser' to have a typedef
;
public loginC (username: string, password: string): Observable<boolean> {
return this.http.post( authURL + loginURL,
--> JSON.stringify({ password: password, username: username }))
.map((response: Response) => {
let token: string = response.json() && response.json().token;
if (token) {
this.token = token;
--> localStorage.setItem("currentUser", JSON.stringify({token: token, username: username}));
return true;
} else {
return false;
}
});
}
在 password: password, username: username
中:message: 'Expected property shorthand in object literal.
我真的明白这一点。最后我可以写一个简单的 model: any {}
;
export class LoginComponent {
--> public model: any = {};
public loading: boolean = false;
public error: string = "";
constructor (private router: Router, private authenticationService: ServerDataComponent) {
//
}
public login(): void {
this.loading = true;
this.authenticationService.loginC(this.model.username, this.model.password)
.subscribe(result => {
--> if (result === true) {
this.router.navigate(["/table_per"]);
} else {
this.error = "Введен неверный логин и/или пароль";
this.loading = false;
}
});
}
对于任何 - Type declaration of 'any' is forbidden
;
Ror 结果 - expected arrow-parameter: 'result' to have a typedef
对于 expected variable-declaration: 'currentUser' to have a typedef
,您可以为自定义类型定义 interface
。
export interface User {
token: string;
}
并用它来设置类型。
let currentUser: User = JSON.parse(localStorage.getItem("currentUser"));
对于 Expected property shorthand in object literal
,当键名与变量名匹配时,您可以使用 shorthand 语法。
JSON.stringify({token, username})
对于Type declaration of 'any' is forbidden
,您可以尝试将类型更改为Object
。如果不是,您需要为您的模型声明另一个 interface
。
public model: Object = {};
对于expected arrow-parameter: 'result' to have a typedef
你需要设置参数的类型。
.subscribe((result: boolean) => {
我的代码有几个错误。我正在使用 Angular 2 + TSLint:
constructor(http: Http) {
this.http = http;
--> let currentUser = JSON.parse(localStorage.getItem("currentUser"));
this.token = currentUser && currentUser.token;
}
在当前用户中我有这个错误:message: 'expected variable-declaration: 'currentUser' to have a typedef
;
public loginC (username: string, password: string): Observable<boolean> {
return this.http.post( authURL + loginURL,
--> JSON.stringify({ password: password, username: username }))
.map((response: Response) => {
let token: string = response.json() && response.json().token;
if (token) {
this.token = token;
--> localStorage.setItem("currentUser", JSON.stringify({token: token, username: username}));
return true;
} else {
return false;
}
});
}
在 password: password, username: username
中:message: 'Expected property shorthand in object literal.
我真的明白这一点。最后我可以写一个简单的 model: any {}
;
export class LoginComponent {
--> public model: any = {};
public loading: boolean = false;
public error: string = "";
constructor (private router: Router, private authenticationService: ServerDataComponent) {
//
}
public login(): void {
this.loading = true;
this.authenticationService.loginC(this.model.username, this.model.password)
.subscribe(result => {
--> if (result === true) {
this.router.navigate(["/table_per"]);
} else {
this.error = "Введен неверный логин и/или пароль";
this.loading = false;
}
});
}
对于任何 - Type declaration of 'any' is forbidden
;
Ror 结果 - expected arrow-parameter: 'result' to have a typedef
对于 expected variable-declaration: 'currentUser' to have a typedef
,您可以为自定义类型定义 interface
。
export interface User {
token: string;
}
并用它来设置类型。
let currentUser: User = JSON.parse(localStorage.getItem("currentUser"));
对于 Expected property shorthand in object literal
,当键名与变量名匹配时,您可以使用 shorthand 语法。
JSON.stringify({token, username})
对于Type declaration of 'any' is forbidden
,您可以尝试将类型更改为Object
。如果不是,您需要为您的模型声明另一个 interface
。
public model: Object = {};
对于expected arrow-parameter: 'result' to have a typedef
你需要设置参数的类型。
.subscribe((result: boolean) => {