api get后从服务发送消息

时间:2017-06-30 01:51:19

标签: angular typescript

我的post.service通过我的area.service中的MessageService收到一条消息,我已设置为接收任何类型的主题。 我试图通过message.service将area.service的响应传达给post.service。 邮政服务网址应为http://localhost:8000/areas/fun/ 区域服务应该发送“有趣”。

post.service

    import { Injectable, OnDestroy } from '@angular/core';
        import { Http, Headers, RequestOptions, Response } from '@angular/http';
        import { Observable, Subject, Subscription } from 'rxjs';
        import 'rxjs/add/operator/map';

        import { AuthenticationService, AreaService, MessageService} from './index';
        import { Post, Area} from '../_models/index';

        @Injectable()
        export class PostService implements OnDestroy {
          areas: Area[] = [];
          subject:Subject<Post[]> = new Subject();
          message: any;
       subscription: Subscription;
            constructor(
                private http: Http,
                private authenticationService: AuthenticationService,
                private areaService: AreaService,
                private messageService: MessageService
              ) {
                this.subscription = this.messageService.getMessage().subscribe(message => { this.message = message;});
            }

            getPosts(): Observable<Post[]> {
              this.areaService.getAreas();

            // add authorization header with jwt token
            let headers = new Headers();
            headers.append('Authorization','token ' + this.authenticationService.token);

            headers.append('Content-Type', 'application/json');
          let options = new RequestOptions({

          headers: headers
        });

            // get posts from api
            return this.http.get('http://localhost:8000/areas/'+ this.message.text +'/', options)
                .map((response: Response) => response.json());

        }
        ngOnDestroy() {
            // unsubscribe to ensure no memory leaks
            this.subscription.unsubscribe();
        }
      }

area.service

    import { Injectable } from '@angular/core';
    import { Http, Headers, RequestOptions, Response } from '@angular/http';
    import { Observable } from 'rxjs';
    import 'rxjs/add/operator/map';

    import { AuthenticationService, MessageService } from './index';
    import { Area } from '../_models/index';

    @Injectable()
    export class AreaService {
      public areas: Area[] = [];
      public currentArea: string = "fun";
        constructor(
            private http: Http,
            private authenticationService: AuthenticationService,
          private messageService: MessageService) {
        }

        getAreas(): void {
            // add authorization header with jwt token
            let headers = new Headers();
            headers.append('Authorization','token ' + this.authenticationService.token);

            headers.append('Content-Type', 'application/json');
          let options = new RequestOptions({
          headers: headers
          });

            // get areas from api
             this.http.get('http://localhost:8000/areas/', options)
                .map((response: Response) => {

                  this.areas = response.json();
                  this.messageService.sendMessage(this.areas[0].name);

                });
    //this.messageService.sendMessage(this.areas[0].name);
        }
    }

message.service

    import { Injectable } from '@angular/core';
    import { Observable } from 'rxjs';
    import { Subject } from 'rxjs/Subject';

    @Injectable()
    export class MessageService {
        private subject = new Subject<any>();

        sendMessage(message: any) {
            this.subject.next({ text: message });
        }

        clearMessage() {
            this.subject.next();
        }

        getMessage(): Observable<any> {
            return this.subject.asObservable();
        }
    }

它告诉我的错误是“错误:错误:0:0导致:无法读取未定义的属性'text'”

任何帮助将不胜感激

0 个答案:

没有答案