在构造函数中使用 Http 的 Student Service 方法示例

import {Http} from '@angular/http';
@Injectable()
export class StudentService{
    constructor(public http: Http){}
    getAllStudents(): Observable<Students[]>{
        return this.http.get('assets/students.json')
        .map(res => res.json().data)     
        }
    }

现在再次注意构造函数如果我们想要使用这个服务方法,我们将转到我们的视图/页面并:

import {StudentService} from './student.service';
import { SocialSharing } from '@ionic-native/social-sharing';
export class HomePage implements OnInit {

  constructor(public _studentService: StudentService, public socialSharing: SocialSharing) {
   }

再次注意这里的构造函数,我们在构造函数中创建了一个 StudentService 实例,还有一件事,我们使用的是 socialSharing 插件,所以我们也在构造函数中创建它的实例。