什么做打字稿语法&#34; <someclass>&#34;作为参数意味着什么?

时间:2017-01-02 15:27:13

标签: javascript angular typescript

我正在阅读angular2代码,我发现了一些令人困惑的语法。 完整代码如下(来自https://github.com/domfarolino/angular2-login-seed

var event = {
    start: startEvent,
    end: endEvent,
    title: "Test",
    allDay: true
}

我无法找到以下代码作为参数的含义。

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");
    }
}

有人可以给我一个想法吗?

2 个答案:

答案 0 :(得分:0)

类在这里作为数据类型工作。 ..   例如

班级学生{ 名称:字符串, RollNo:数字 }

现在如果我声明一个变量

公立学生:观察

现在我可以用一个名字和滚动号码的对象推送学生。

答案 1 :(得分:0)

语法<Type> variable是演员表。见Type Assertions on the documentation

  

有时你最终会遇到一个比TypeScript更了解价值的情况。通常,当您知道某个实体的类型可能比其当前类型更具体时,就会发生这种情况。

     

类型断言是告诉编译器的一种方式“相信我,我知道我在做什么。”类型断言就像在其他语言中使用类型转换,但不执行特殊的数据检查或重组。它没有运行时影响,纯粹由编译器使用。 TypeScript假定您(程序员)已执行了您需要的任何特殊检查。

它显示了两个例子,可以使用:

进行投射
<string> somevar

以及

somevar as string
  

两个样本是等价的。使用一个而不是另一个主要是选择偏好;但是,在使用带有JSX的TypeScript时,只允许使用as样式的断言。

相关问题