2017-01-02 64 views
-1

我正在閱讀angular2代碼,我發現了一些令人困惑的語法。 完整的代碼如下。(從https://github.com/domfarolino/angular2-login-seed作爲參數的打字稿語法「<SomeClass>」是什麼意思?

import { Injectable, Inject } from '@angular/core'; 
//import { Control } from '@angular/common'; 
import { Http, Response, Headers, RequestOptions, RequestOptionsArgs } from '@angular/http'; 
import { Observable } from 'rxjs/Observable'; 
import 'rxjs/add/observable/throw'; 

/** 
* Import interfaces that service depends on 
*/ 
import { User } from './user'; 

@Injectable() 
export class UserService { 
    constructor (private http: Http, @Inject('apiBase') private _apiBase: string) { 

    } 

    private _loginApi = this._apiBase + '/authorize/local'; 
    private _logoutApi = this._apiBase + '/logout'; 
    private _authenticatedApi = this._apiBase + '/api/authenticated'; 
    private _registerApi = this._apiBase + '/api/users/register'; 
    private _userExistsApi = this._apiBase + '/api/users/exists'; 

    login(user) { 
     let body = JSON.stringify(user); 
     let headers = new Headers(); 
     headers.append('Content-Type', 'application/json'); 

     return this.http.post(this._loginApi, body, <RequestOptionsArgs> {headers: headers, withCredentials: true}) 
         .map((res: Response) => res) 
         .catch(this.handleError); 
    } 

    authenticated() { 
     return this.http.get(this._authenticatedApi, <RequestOptionsArgs> {withCredentials: true}) 
         .map((res: Response) => res.json()) 
         .catch(this.handleError); 
    } 

    logout() { 
     return this.http.get(this._logoutApi, <RequestOptionsArgs> {withCredentials: true}) 
         .map((res: Response) => res.json()) 
         .catch(this.handleError); 
    } 

    register(user) { 
     let body = JSON.stringify(user); 
     let headers = new Headers(); 
     headers.append('Content-Type', 'application/json'); 

     return this.http.post(this._registerApi, body, <RequestOptionsArgs> {headers: headers, withCredentials: true}) 
         .map((res: Response) => res) 
         .catch(this.handleError); 
    } 

    getUsers() { 
     return this.http.get(this._apiBase + "/api/users?limit=5&desc=true", <RequestOptionsArgs> {withCredentials: true}) 
        .map((res: Response) => res.json()) 
        .catch(this.handleError); 
    } 

    getMe() { 
     return this.http.get(this._apiBase + '/api/users/me/', <RequestOptionsArgs> {withCredentials: true}) 
        .map((res: Response) => res.json().me) 
        .catch(this.handleError); 
    } 

    private handleError (error: Response) { 
     // in a real world app, we may send the server to some remote logging infrastructure 
     // instead of just logging it to the console 
     return Observable.throw(error || "Server Error"); 
    } 
} 

,我無法找到作爲參數意味着下面的代碼是什麼。

<RequestOptionsArgs> {headers: headers, withCredentials: true} 

有沒有人可以給我一個想法?

+1

基本上,它被鑄造成這種類型。 –

+0

謝謝!我遇到了麻煩,因爲我不知道我應該使用什麼關鍵字來搜索谷歌。 – Ko01

回答

0

語法<Type> variable是演員。請參閱Type Assertions on the documentation

有時您最終會發現您會比TypeScript更瞭解某個值。通常這會發生在你知道某個實體的類型可能比其當前類型更具體時。

類型斷言是告訴編譯器「相信我,我知道我在做什麼」的一種方式。類型斷言就像其他語言中的類型轉換,但不執行特殊的檢查或重構數據。它沒有運行時影響,純粹由編譯器使用。 TypeScript假定您,程序員已經執行了您需要的任何特殊檢查。

它顯示了兩個例子,也可以使用澆鑄:

<string> somevar 

並且還與

somevar as string 

兩種樣品是相同的。使用其中之一大多是一種偏好選擇;但是,在JSX中使用TypeScript時,只允許使用as-style斷言。

+0

感謝您的好解釋。 – Ko01

0

類在此處作爲數據類型工作。例如: Ex。

類學生{ 名稱:字符串, RollNo:數 }

現在,如果我聲明一個變量

公共學生:Obect

現在我可以在學生推動與具有對象名稱和卷號