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.log
中getEmail
函数内的两个history.service.ts
正常工作,但console.log
函数中的HistoryList
返回undefined
。为什么会这样?
答案 0 :(得分:0)
UserProfileService.getEmail()从未在HistoryList()中被调用,这就是为什么UserProfileService.title在您登录时未定义的原因。
答案 1 :(得分:0)
这是错误的方法,您可以使用此方法将值分配给服务中的变量。
this.UserProfileService.Title=someValue;