[英]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;
}
在currentUser中,我有此錯誤: 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
當鍵名與變量名匹配時,可以使用速記語法。
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) => {
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.