等待Api的响应-Angular js 2

等待Api的响应-Angular js 2,angular,typescript,cookies,Angular,Typescript,Cookies,我想在cookie中放置一个令牌唯一的问题是cookie是在获得API响应之前创建的,我如何等待响应然后创建cookie 我的代码 getlogin() { this._symfonyservice.login(this.user,this.pass).subscribe(//call the post data => this.results = data, // put the data returned from the server in our varia

我想在cookie中放置一个令牌唯一的问题是cookie是在获得API响应之前创建的,我如何等待响应然后创建cookie

我的代码

getlogin()
{
this._symfonyservice.login(this.user,this.pass).subscribe(//call the post
            data => this.results = data, // put the data returned from the server in our variable
            error => console.log("Error HTTP Post Service"), // in case of failure show this message
            () => console.log(this.results['token'])//run this code in all cases
        );
if(this.results['token']!="")
{
    let token = this.results['token'];
    this.deleteCookie("Cookieteste");
    this.setCookie("Cookieteste",token, 1);
}
   }

数据中执行,而不是在
()最终函数中执行

getlogin()
{
this._symfonyservice.login(this.user,this.pass).subscribe(//call the post
data => {
            this.results = data
            if(this.results['token']!="")
            {
                let token = this.results['token'];
                this.deleteCookie("Cookieteste");
                this.setCookie("Cookieteste",token, 1);
            }       
        }, // put the data returned from the server in our variable
                error => console.log("Error HTTP Post Service"), // in case of failure show this message
                () => console.log('completed')//run this code in all cases
     );
subscribe方法仅用于post请求,对吗?或者我可以使用它来获取请求,然后使用数据来执行代码。您可以将它用于所有的get、post、put和delete。