import { Injectable } from '@angular/core'; import { Http, Headers, RequestOptions, Response } from
2017-07-12 14:29
931 查看
登录 http://47.92.39.225:8080/ 查看更多文章
定义一个Angular2/4 service:
import { Injectable } from '@angular/core';import { Http, Headers, RequestOptions, Response } from '@angular/http';import { User } from '../_models/index';@Injectable()export class UserService {constructor(private http: Http) { }getAll() {return this.http.get('/api/users', this.jwt()).map((response: Response) => response.json());}getById(id: number) {return this.http.get('/api/users/' + id, this.jwt()).map((response: Response) => response.json());}create(user: User) {return this.http.post('/api/users', user, this.jwt()).map((response: Response) => response.json());}update(user: User) {return this.http.put('/api/users/' + user.id, user, this.jwt()).map((response: Response) => response.json());}delete(id: number) {return this.http.delete('/api/users/' + id, this.jwt()).map((response: Response) => response.json());}// private helper methodsprivate jwt() {// create authorization header with jwt tokenlet currentUser = JSON.parse(localStorage.getItem('currentUser'));if (currentUser && currentUser.token) {let headers = new Headers({ 'Authorization': 'Bearer ' + currentUser.token });return new RequestOptions({ headers: headers });}}}
相关文章推荐
- from django.http import HttpResponseRedirect
- from torch._C import * ImportError: numpy.core.multiarray failed to import
- 理解Angular的providers给Http添加默认headers
- import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;报错
- docker export import后,导入镜像,启动时的错误,Error response from daemon: No command specified
- The type org.apache.http.HttpResponse cannot be resolved. It is indirectly referenced from required
- HTTP Response Headers
- [Angular] Handle HTTP Errors in Angular with HttpErrorResponse interface
- The type org.apache.http.HttpResponse cannot be resolved. It is indirectly referenced from required
- angular在HTML中去取字符串,以及$http通过headers取值
- 解决 docker: Error response from daemon: ... : net/http: TLS handshake timeout.
- Angular 4/2 Http get() Parameters + Headers + URLSearchParams + RequestOptions Exam
- The type org.apache.http.HttpResponse cannot be resolved. It is indirectly referenced from required
- [Angular] Set Metadata in HTTP Headers with Angular HttpHeaders
- java.lang.IllegalArgumentException: not found @HttpResponse from class java.lang.Object解决方法
- View HTTP Request/Response Headers in Browser
- c++利用libcurl获取http网页的 response headers 等信息
- [Angular] Read Custom HTTP Headers Sent by the Server in Angular
- Java Code Examples for com.sun.net.httpserver.HttpExchange.getResponseHeaders()
- 如何通过Python+Selenium+PhantomJS/Chrome获取HTTP状态和Response Headers