我的组件中有代码
export class AddNewCardComponent {
public concept = [];
constructor(
private _router: Router,
private _empDiscService: empDiscService) {
}
ngOnInit(){
this.concept = this._empDiscService.StoreMaster()
.subscribe((data)=>{
this.concept = data;
});
}
}
它只是我想从服务中获取var并将其放入我的商店= [];
但它说
类型void不能分配给任何
类型
这是我的服务代码
import {Injectable} from '@angular/core';
import { Httpprovider } from './httpprovider';
@Injectable()
export class empDiscService{
public storedata = [];
constructor(private _httpprovider: Httpprovider){
}
StoreMaster():Observable<any[]>{
return this._httpprovider.httpReq('http://192.168.1.40:5000/getconcept','GET',null,null).map(res =>{<any[]>res.json()}
};
}
答案 0 :(得分:2)
import { JobService } from './../services/job.service';
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-job-list',
templateUrl: './job-list.component.html',
styleUrls: ['./job-list.component.css']
})
export class JobListComponent implements OnInit {
**jobs:any = [];**
erreur = '';
constructor( private jobservice:JobService) { }
ngOnInit() {
this.jobservice.getJobs().subscribe(
data => this.jobs = data,
error => {console.log ('error')}
);
}
}
答案 1 :(得分:0)
您不得subscribe()
在服务中并直接返回对象,这不是 RxJs 的目的。
export class empDiscService{
public storedata = [];
constructor(private _httpprovider: Httpprovider){
}
StoreMaster():Observable<any[]>{
return this._httpprovider.httpReq('http://192.168.1.40:5000/getmapmasterstore','GET',null,null).map(res =>{<any[]>res.json()}
}
}
您的组件应该是
constructor(
private _router: Router,
private _empDiscService: empDiscService) {
this.store = this._empDiscService.StoreMaster()
.subscribe((data)=>{
this.storedata = data;
});
}
你犯的实际错误是服务方法没有返回任何内容
StoreMaster():Observable<any[]>{}
这将解决您的问题
答案 2 :(得分:0)
StoreMaster()方法不返回任何内容。您可能需要更改服务方法以返回observable:
StoreMaster(){
return this._httpprovider.httpReq('http://192.168.1.40:5000/getmapmasterstore','' +
' GET',null,null).subscribe((data)=>{
var brData = [];
for (let i=0;i<data.length;i++){
brData.push(data[i]);
}
return brData;
});
}
和ngOnInit
ngOnInit(){
this._empDiscService.StoreMaster()
.subscribe(store => this.store = store);
}