【问题标题】:Http call are not happening in this code?这段代码中没有发生 Http 调用?
【发布时间】:2017-12-28 11:01:47
【问题描述】:

核心服务:

export class CoreService {

  constructor(private _http: HttpClient) { 
    Common.Socket.next('https://reqres.in');
  }

  httpget(url): any {
    return Common.Socket
    .switchMap((x)=> this._http.get( x + url));
  }
  httppost(url, body): any {
    return Common.Socket
    .switchMap((x)=> this._http.post( x + url, body, 
    {headers: this.createAuthorizationHeader()}
    ));
  }

  private createAuthorizationHeader() {
    const headers = new HttpHeaders({'Content-Type':'application/json'});
    return headers;
  }
}

组件服务:

export class AppServiceService {

      constructor( private _coreService: CoreService) { }
      getAll(path: string){
        return this._coreService.httpget(path);
      }
     getParticular(url){
        return this._coreService.httpget(url);
      }
      create(url, body) {
        return this._coreService.httppost(url, body);
      }
    }

APP模块:

@NgModule({
  imports:      [ BrowserModule, FormsModule, HttpClientModule ],
  declarations: [ AppComponent ],
  bootstrap:    [ AppComponent ],
  providers: [CoreService]
})
export class AppModule { }

应用组件

    export class AppComponent  implements OnInit {
      name = 'Angular 5';
      public allPosts: any;
      constructor(private _appServiceService: AppServiceService) {
      }
      ngOnInit(): void {
        this.getAllPosts();
      }
      private getAllPosts(): void {
        this._appServiceService.getAll('/api/users?page=2').subscribe(
          (res) => {
            this.allPosts = res;
            console.log(this.allPosts, 'allposts');
          }
        );
      }
      getParticular(): void {
        this._appServiceService.getParticular('/api/users/2').subscribe(
          (res) => {
            this.allPosts = res;
            console.log(this.allPosts, 'allposts');
          }
        );
      }
createMessage() {
    const url = '/api/users';
    const body = { 'name': 'morpheus-1','job': 'leader'};
    this._appServiceService.create(url, body).subscribe(
      (x) => {
        console.log(x);
      }
    );
  }
    }

iam 从 app.component.html 中的 click 事件调用 create 和 getparticular。当我发出请求时,核心服务没有发出 http 调用。请为此提供解决方案。我认为 rxjs 导致了这个问题。 我在这里转载->https://stackblitz.com/edit/angular-rcxgxv.

【问题讨论】:

    标签: angular rxjs angular-services


    【解决方案1】:

    当你添加时它正在工作

    Common.Socket.next('https://reqres.in');
    

    在 CoreService 的每个函数中。 而不是使用主题作为 url 只是使用字符串。

    import { Subject } from 'rxjs/Subject';
    export class Common {
      static Socket = "https://reqres.in";
    }
    

    然后在CoreService中添加一些改动:

    @Injectable()
    export class CoreService {
    
      constructor(private _http: HttpClient) { 
      }
    
      httpget(url): any {
        return this._http.get( Common.Socket.toString() + url);
      }
      httppost(url, body): any {
        return this._http.post(Common.Socket.toString() + url, body,  
        {headers: this.createAuthorizationHeader()}
        );
      }
    
      private createAuthorizationHeader() {
        const headers = new HttpHeaders({'Content-Type':'application/json'});
        return headers;
      }
    }
    

    同样在 AppComponent 中,当您发布消息时添加订阅功能,因为当 http.post 上没有订阅或承诺时,Angular 4 不会发送帖子请求

      createMessage() {
        const url = '/api/users';
        const body = { 'name': 'morpheus-1', 'job': 'leader' };
        this._appServiceService.create(url, body).subscribe();
      }
    

    【讨论】:

    • 其实我会做一个休息调用获取端点。即在执行 coreservice 方法之前,我将调用 rest 来获取端点(通常在构造函数中)。 Common.Socket.next('reqres.in'); -> 这个端点(reqres.in)是动态的。
    • 根据这个答案,端点是静态的。我想让它动态化。
    • 这样您就可以使用获取端点数据并存储它的服务,这些数据将被注入。它可能具有带有端点数据和功能的对象。当您调用此函数时,端点对象将填充数据。你可以只做exampleService.endpoints.allUsers。您可以在 exampleService 构造函数中调用获取端点的函数,并且端点数据将可用于您的所有服务。
    • 我在同一个堆栈闪电战中进行了更改。根据上述评论stackblitz.com/edit/angular-rcxgxv是否正确。
    • 我尝试使用动态端点,其中核心服务(派生类)方法在从端点(父类)获得响应之前执行。所以我得到 404 因为 url 未定义。
    猜你喜欢
    • 1970-01-01
    • 2016-06-09
    • 1970-01-01
    • 1970-01-01
    • 2018-07-28
    • 1970-01-01
    • 1970-01-01
    • 2017-04-23
    相关资源
    最近更新 更多