简体   繁体   English

Angular 6-不建议使用的方法

[英]Angular 6 - Deprecated methods

I'm trying to change the request options but it's deprecated. 我正在尝试更改请求选项,但已弃用。 I can't find the option for this. 我找不到这个选项。

Any help? 有什么帮助吗?

import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http'
import { HttpHeaders} from "@angular/common/http";
import {RequestOptions} from "@angular/http";

@Injectable({
  providedIn: 'root'
})
export class UserService {
  private baseUrl:string = 'http://localhost:8080/api';
  private headers = new HttpHeaders({'Content-Type':'application/json'});
  private options = new RequestOptions({headers:this.headers});

  constructor() { }
}

I think this is just an import problem, because they moved these options to another place. 我认为这只是一个导入问题,因为他们将这些选项移到了另一个地方。 The documentation states: 该文档指出:

use @angular/common/http instead 使用@ angular / common / http代替

So I guess you just have to import the options from @angular/common/http instead of @angular/http 所以我想你只需要从@angular/common/http而不是@angular/http导入选项

EDIT 编辑

I should have looked a bit closer. 我应该近一点看。 Headers can now be send a bit differently than before, you don't have to use RequestOptions anymore, just pack it up as a simple object. 现在发送标头的方式可能与以前有所不同,您不必再使用RequestOptions了,只需将其打包为一个简单的对象即可。 In your case it could look like this: 在您的情况下,它可能看起来像这样:

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

Then you can use these options with your http methods. 然后,您可以将这些选项与http方法一起使用。 Here's an example from angular's fundamentals : 这是来自angular的基础知识的示例:

addHero (hero: Hero): Observable<Hero> {
  return this.http.post<Hero>(this.heroesUrl, hero, httpOptions)
    .pipe(
      catchError(this.handleError('addHero', hero))
    );
}

Deprecated APIs and Features 不推荐使用的API和功能

Headers -> HttpHeaders
RequestOptions -> HttpRequest
HttpModule -> HttpClientModule

Old

import {HttpModule, Headers, RequestOptions} from "@angular/http";

New

import { HttpClientModule, HttpRequest, HttpHeaders } from "@angular/common/http";

Refer the following URL for more details: https://angular.io/guide/deprecations 请参阅以下URL以获取更多详细信息: https : //angular.io/guide/deprecations

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

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