2016-11-23 3 views
0

У меня есть несколько ошибок в моем коде. Я использование Угловой 2 + TSLint:Ошибки TSLint в угловом 2

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. Я понимаю это. И finalli я могу написать простой 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

ответ

1

Для 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) => { 
+0

я добавить 'Object', и теперь,' токен недвижимости не существует типа Object' –

+0

справа, обновленный с 'interface' определения. – Puigcerber

+0

добавить это в конструктор? –

Смежные вопросы