NG-ZORRO / rebirth-http

Java JPA like HTTP client for Angular. 🍙
MIT License
57 stars 12 forks source link

您好,问个问题 #6

Closed jiayisheji closed 7 years ago

jiayisheji commented 7 years ago

您好,我用您这个http之后发现请求2次,我用angular自带http,只有一次请求,这是什么情况

bangwu commented 7 years ago

可以贴出你的请求代码吗?

jiayisheji commented 7 years ago

我的代码按rebirth上面写的,一模一样,不然我也不知道这东西

greengerong commented 7 years ago

应该是你代码问题。贴出来看看吧。 @jiayisheji

jiayisheji commented 7 years ago

@greengerong

定义HomeService

import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
import { Observable } from 'rxjs/Observable';
import { RebirthHttp, RebirthHttpProvider, GET} from 'rebirth-http';

export abstract class HomeService extends RebirthHttp {

  abstract getArticles(pageIndex, pageSize, keyword?: string): Observable<any>;

}

@Injectable()
export class GithubArticleService extends HomeService {
  constructor(protected http: Http, protected rebirthHttpProvider: RebirthHttpProvider) {
    super();
  }
  getArticles(pageIndex = 1, pageSize = 10, keyword?: string): Observable<any> {
    return this.innerGetArticles()
      .map(res => {
        const result = res.result || [];
        const startIndex = (pageIndex - 1 ) * pageSize;
        return {
          pageSize,
          pageIndex,
          total: result.length,
          result: result.slice(startIndex, startIndex + pageSize)
        };
      });
  }

  @GET('list.json')
  private  innerGetArticles(): Observable<any> {
    return null;
  }
}

export const REBIRTH_ARTICLE_SERVICE_PROVIDERS: Array<any> = [
  {
    provide: HomeService,
    useClass: GithubArticleService
  }
];

调用HomeService

import { Component, OnInit } from '@angular/core';
import { HomeService } from './home.service';

@Component({
  selector: 'app-home',
  templateUrl: './home.component.html',
  styleUrls: ['./home.component.css']
})
export class HomeComponent implements OnInit {

  constructor(private homeService: HomeService) { }

  ngOnInit() {}

  getList() {
    this.homeService.getArticles(1, 10).subscribe(result => {
      console.log(result);
    }, (e) => console.log(e, 'ArticleListComponent error'));
  }

}
greengerong commented 7 years ago

代码看不错具体问题。 你可以chrome调试下。

jiayisheji commented 7 years ago

@greengerong 好的,谢谢

greengerong commented 7 years ago

是不是你说的跨域请求?一个option,一个真实get/post? @jiayisheji

jiayisheji commented 7 years ago

@greengerong 我是跨域了,请求结果是2个option,2个真实get/post。如果是一个option,一个真实get/post,我就不会有这个疑问了

greengerong commented 7 years ago

恩, 你在调试下看看

jiayisheji commented 7 years ago

嗯,好的