简体   繁体   English

无法将数据从服务传递到Angular中的组件

[英]Unable to pass data from Service to a Component in Angular

I am trying to pass data from my service to my component but unable to do so. 我试图将数据从服务传递到组件,但无法这样做。 Below is the code from service and component: 以下是来自服务和组件的代码:

authService.service.ts authService.service.ts

import { Injectable } from '@angular/core';
import { HttpHeaders } from '@angular/common/http';
import{Http, RequestOptions,Headers} from '@angular/http'
import { Observable} from 'rxjs';
import { URLSearchParams,Response } from '@angular/http';


 @Injectable()

  export class AuthenticationService
    {
         constructor(private http: Http) { }
         username:string = 'Admin';
         password:string='livelink';  
         result :any;

        login() 
         {

           let urlSearchParams = new URLSearchParams();
           urlSearchParams.append('Username', this.username);
           urlSearchParams.append('Password', this.password);

    return this.http.post('http://localhost/otcs/cs.exe/api/v1/auth',urlSearchParams)
        .subscribe((res:Response) => 
        {               
            const data = res.json();                                    
            let headers = new Headers({
                'OTCSTICKET': data['ticket']
            });

            let request_options = new RequestOptions({ headers:headers});

           return this.http.get("http://localhost/otcs/cs.exe/api/v1/nodes/16236/output",request_options)          
               .subscribe(data =>
               {    
                   const report = data.json();
                   this.result = report;
                   console.log(this.result);             
               },
                err => {
                    console.log(err);                
                }


       )});


   }
}

App Component 应用组件

      import { Component } from '@angular/core';
      import { AuthenticationService } from './authNew.servive';
      import { HttpClient, HttpHeaders } from '@angular/common/http';
      import{Http, RequestOptions,Headers} from '@angular/http'
      import { Observable} from 'rxjs';
      import { URLSearchParams,Response } from '@angular/http';

      @Component({
                selector: 'app-root',
                templateUrl: './app.component.html',
                styleUrls: ['./app.component.css']
                 })

           export class AppComponent 
         {

             constructor(private authenticationservice:AuthenticationService)
              {

                this.authenticationservice.login().subscribe(res:Response =>
                  {
                    //somecode
                   }) ;

            }

         }

The Problem is when I am trying to subscribe in AppComponent, I get the below error: 问题是当我尝试订阅AppComponent时,出现以下错误:

在此处输入图片说明

I understand I am doing something really silly here. 我知道我在这里做的事真傻。 Also I tried to use map instead of subscribe in my service for both the POST and GET calls but in that case i get an error as sown below: 我也尝试使用地图而不是在服务中为POST和GET调用订阅,但是在这种情况下,出现如下所示的错误:

在此处输入图片说明

I was importing 'rxjs/add/operator/map'; 我正在导入'rxjs / add / operator / map'; when trying to use map and still I was getting the error, hence i thought about subscribing within the service itself but that isn't helping either. 当尝试使用地图时,仍然出现错误,因此我想到了在服务本身内进行预订,但这都无济于事。 Please help me in understanding how can I get this sorted and get data from my service to be available in AppComponent. 请帮助我了解如何进行排序并从服务中获取数据以在AppComponent中可用。

Thanks ! 谢谢 !

You are subscribing twice, in the service and in the component. 您在服务和组件中都订阅了两次。

You have in the official tutorial how should be managed this scenario: 在官方教程中,您具有在这种情况下应如何管理:

Tour of heroes http 英雄之旅http

The resume is that you should get the observable in your service 简历是您应该在服务中得到可观察到的

return this.http.get("http://localhost/otcs/cs.exe/api/v1/nodes/16236/output",request_options);   

and manage the subscribe in the component: 并管理组件中的订阅:

this.authenticationservice.login().subscribe(res:Response =>
                  {
                    //somecode
                   }) ; 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM