无法从服务类中获得价值

时间:2019-05-11 07:16:53

标签: angular

component.ts文件:

import { Component, OnInit, Input } from '@angular/core';
import { HistoryService } from '../../../../shared/history.service';
import { Router } from "@angular/router";
import { NgForm } from '@angular/forms';
import { History } from '../../../../shared/history.model';

@Component({
  selector: 'app-ins-history',
  templateUrl: './ins-history.component.html',
  styleUrls: ['./ins-history.component.css'],
  providers:[HistoryService]
})

export class InsHistoryComponent implements OnInit {

  constructor(public UserProfileService: HistoryService, private router: Router) { }


  ngOnInit() {

    this.HistoryList();
  }

  HistoryList()
  { let title = this.UserProfileService.Title;
      console.log(title);
      this.UserProfileService.getHistoryList(title).subscribe((res)=> {
      this.UserProfileService.history= res as History[];
      alert('ok');

    });
  }

console.loggetEmail函数内的两个history.service.ts正常工作,但console.log函数中的HistoryList返回undefined。为什么会这样?

history.service.ts

2 个答案:

答案 0 :(得分:0)

UserProfileService.getEmail()从未在HistoryList()中被调用,这就是为什么UserProfileService.title在您登录时未定义的原因。

答案 1 :(得分:0)

这是错误的方法,您可以使用此方法将值分配给服务中的变量。

this.UserProfileService.Title=someValue;